<input type="button" value="Include File" onclick="include('http://www.ourdomain.com/path/filename.txt');">
include(URL[, optionalTargetElementId][, optionalLoadingImageSourcePath]);
//<![CDATA[
// Does this browser support try-catch?
var tc = false;
try {
tc = true;
} catch(f) { }
var xmlHttpError = 'XML HTTP Request: OK';
function getRequestObject() {
var objRequest;
if (window.ActiveXObject) {
if (tc) {
try {
objRequest = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e) {
try {
objRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
catch(f) { }
}
} else {
objRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
} else if (window.XMLHttpRequest) {
objRequest = new XMLHttpRequest();
}
return objRequest;
}
function include(pUrl,pElementId,pImageSrc) {
if (arguments.length==3) {
if (pImageSrc) {
var el = document.createElement('img');
// Add the Please Wait Image
el.setAttribute('src', pImageSrc);
el.setAttribute('alt', 'Please Wait');
el.setAttribute('width', 16);
el.setAttribute('height', 16);
document.getElementById(pElementId).appendChild(el);
}
}
var objRequest = getRequestObject();
if (typeof(objRequest)=='object') {
if (objRequest.readyState>=0) {
objRequest.onreadystatechange = function() { handleHttpResponse(objRequest, pElementId); };
objRequest.open('GET', pUrl, true);
objRequest.send(null);
}else{
xmlHttpError = 'XML HTTP Request Object Unavailable';
document.getElementById(pElementId).innerHTML = xmlHttpError;
return false;
}
}else{
xmlHttpError = 'XML HTTP Request Object Not Supported';
document.getElementById(pElementId).innerHTML = xmlHttpError;
return false;
}
}
function handleHttpResponse(pObjRequest, pElementId) {
if (pObjRequest.readyState==4) {
if (pObjRequest.status==200) {
// Remove the Please Wait Image
for (var idx=0; idx<document.getElementById(pElementId).children.length; idx++) {
el = document.getElementById(pElementId).children.item(idx);
if (el.tagName == 'IMAGE') {
document.getElementById(pElementId).removeChild(el);
}
}
document.getElementById(pElementId).innerHTML=pObjRequest.responseText;
pObjRequest=null;
}
}
}
//-->
//]]>
<script type="text/javascript" src="include.js"></script>
<body>
<noscript><p>This page requires JavaScript to be supported or enabled for complete functionality.</p></noscript>
<p><input type="button" value="Emails.txt" name="b1" onclick="include('http://www.rodsdot.com/javascript/include/emails.txt','theExample','images/loading_icon.gif');">
<input type="button" value="Lincoln.htm" name="b2" onclick="include('http://www.rodsdot.com/javascript/include/lincoln.htm','theExample','images/loading_icon.gif');">
<input type="button" value="Movies.txt" name="b3" onclick="include('http://www.rodsdot.com/javascript/include/movies.txt','theExample','images/loading_icon.gif');">
<input type="button" value="Latin.htm" name="b4" onclick="include('http://www.rodsdot.com/javascript/include/latin.htm','theExample','images/loading_icon.gif');"></p>
<div id="theExample"><iframe name="altFrame" src="http://www.rodsdot.com/javascript/include/latin.htm" scrolling="no" frameborder="0">Your browser does not support inline frames or is currently configured not to display inline frames.</iframe></div>
<script type="text/javascript">
// start by loading latin.htm
include('http://www.rodsdot.com/javascript/include/latin.htm','theExample','images/loading_icon.gif');
</script>
</body>
The example contains a IFRAME for browsers which do not support the remote scripting object. It also contains a <noscript> block for browsers that do not support JavaScript.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Commented:
BTW, did you know that EE itself uses this technique extensively? For instance, on a Zone Landing page, when you click the tabs (such as "Recent Activity", "Awaiting Answer", and "Articles") the script calls a function named AsyncInclude() to populate the listviews. Among other things, the result is a speed up in page load (as measured by, say, Alexa) since all that "hidden stuff" does not weigh down the initial load time.
Author
Commented: