Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts

    including an external file with javascript

    I want to include an external file with javascript, depending on condidtions. browser detection stuff. thephp syntax would be <?php readfile("external.file") ?> and SSI would be <!--#include file="external.file" -->, but is there a way to do it in JavaScript?

    sorry if this question has been asked, i did search with no luck.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The only external file you can include with javascript is an external javascript file with an ".js" extension. The file must contain javascript only. Check this link out for more info:
    http://www.javascriptkit.com/javatutors/external.shtml

  • #3
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    399
    Thanks
    2
    Thanked 30 Times in 30 Posts
    You can try populateing a layer with XMLHTTP.

    You can use this script to bring external include files into a layer in the page, or insert the text inline:

    <script language='JavaScript'>
    <!--
    function include(lyr,url)
    {
    if (document.all)
    {
    // IE version
    try {
    var xml = new ActiveXObject("Microsoft.XMLHTTP");
    // some versions of IE4 and some IE5.0) might be using MSXML2.XMLHTTP.4.0
    xml.Open( "GET", url, false );
    xml.Send()
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    catch (e) {
    var xml = new ActiveXObject("MSXML2.XMLHTTP.4.0");
    xml.Open( "GET", url, false );
    xml.Send()
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    }
    else
    {
    // Mozilla/Netscrap 6+ version
    var xml=new XMLHttpRequest();
    xml.open("GET",url,false);
    xml.send(null);
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    }
    //-->
    </script>
    You can trigger the script off the load:
    <body onLoad="include('somediv','http://somepath/somefile.html'">
    where somediv looks like: <div id="somediv">&nbsp;</div>

    Or dynamically from a link:
    <a href="#" onClick="include('somediv','http://somepath/somefile.html');return false">
    load alternate page
    </a>

    or inline as a conventional include:

    <script language='JavaScript'>
    document.write('<div id="somediv">&nbsp;</div>');
    include('somediv','http://somepath/somefile.html');
    </script>

    If the string returned from the call contains valid HTML it will be rendered, otherwise it will display as plain text.

    Does not work for older browser like Netscrap 4.x that do not know what XML is.

    It can produce an alert if the user security settings have activeX disabled
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #4
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problems in display

    Quote Originally Posted by COBOLdinosaur
    You can try populateing a layer with XMLHTTP.

    You can use this script to bring external include files into a layer in the page, or insert the text inline:

    <script language='JavaScript'>
    <!--
    function include(lyr,url)
    {
    if (document.all)
    {
    // IE version
    try {
    var xml = new ActiveXObject("Microsoft.XMLHTTP");
    // some versions of IE4 and some IE5.0) might be using MSXML2.XMLHTTP.4.0
    xml.Open( "GET", url, false );
    xml.Send()
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    catch (e) {
    var xml = new ActiveXObject("MSXML2.XMLHTTP.4.0");
    xml.Open( "GET", url, false );
    xml.Send()
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    }
    else
    {
    // Mozilla/Netscrap 6+ version
    var xml=new XMLHttpRequest();
    xml.open("GET",url,false);
    xml.send(null);
    document.getElementById(lyr).innerHTML=xml.responseText;
    }
    }
    //-->
    </script>
    You can trigger the script off the load:
    <body onLoad="include('somediv','http://somepath/somefile.html'">
    where somediv looks like: <div id="somediv">&nbsp;</div>

    Or dynamically from a link:
    <a href="#" onClick="include('somediv','http://somepath/somefile.html');return false">
    load alternate page
    </a>

    or inline as a conventional include:

    <script language='JavaScript'>
    document.write('<div id="somediv">&nbsp;</div>');
    include('somediv','http://somepath/somefile.html');
    </script>

    If the string returned from the call contains valid HTML it will be rendered, otherwise it will display as plain text.

    Does not work for older browser like Netscrap 4.x that do not know what XML is.

    It can produce an alert if the user security settings have activeX disabled
    =======================================================

    I used this script successfully with a href="#"OnClick... option.. its working fine but when another page is again loaded with the same href="#"OnClick... the cursor does not move at the top... so the page is not displayed from the top. we have to manually scroll the page ... how to get rid of this??? Please help...

    thanks

    -shubhangi


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •