...

View Full Version : including an external file with javascript



dswimboy
01-31-2004, 10:34 PM
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.

requestcode
01-31-2004, 11:20 PM
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

COBOLdinosaur
01-31-2004, 11:28 PM
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

shkamat
03-07-2006, 06:09 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum