
Originally Posted by
djr33
Ajax brings in the pages as text, not as loading a new page. In other words, you won't be able to use external files referenced in the other page like this-- both css and js will be ignored.
Daniel is right about that, except that we can force Ajax to automatically bring in external code by not only using innerHTML and/or appendChild, but also document.write.
Here's a working example:
Code:
<head>
<script type="text/javascript">
//defining the request
var pageRequest = false //variable to hold ajax object
/*@cc_on
@if (@_jscript_version >= 5)
try {
pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try {
pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e2){
pageRequest = false
}
}
@end
@*/
if (!pageRequest && typeof XMLHttpRequest != 'undefined')
pageRequest = new XMLHttpRequest()
function remove(id)
{
while(document.getElementById(id).firstChild)
{document.getElementById(id).removeChild(document.getElementById(id).firstChild);}
}
function include(content,id){
var newdiv = document.createElement("div");
newdiv.innerHTML = content;
remove(id); document.getElementById(id).appendChild(newdiv);
}
if (pageRequest){
// add as many requests as you want; the variables holding them must be document.written (see below) in order to bring in external CODE
pageRequest.open('GET', 'external1.html', false)
pageRequest.send(null); ajax_include1=pageRequest.responseText;
pageRequest.open('GET', 'external2.html', false)
pageRequest.send(null); ajax_include2=pageRequest.responseText;
document.write('<div style="display:none">');
document.write(ajax_include1+ajax_include2);
document.write('<\/div>');
}
</script>
</head>
<body >
<a onclick='include(ajax_include1,"div1");'>add external1.html</a><br>
<a onclick='include(ajax_include2,"div2");'>add external2.html</a><br><br>
<a onclick='remove("div1")'>remove external1.html</a><br>
<a onclick='remove("div2")'>remove external2.html</a><br>
<div id="div1" ></div>
<div id="div2" ></div>
</body>
Important note: the js belonging to the external files must be (made) external in order for this to function properly in IE.
===
Arie Molendijk.
Bookmarks