Helpful Information
 
 
Category: Coding tips & tutorials threads
Alternative to AJAX

I'm afraid Ajax is not a good tool for including external content.
The following script works in non-IE, but not in IE6/7 (appendChild problem in IE):

HEAD:
<script type="text/javascript">
AjaxInclude = function (url) {
if (window.XMLHttpRequest) {Ajax=new XMLHttpRequest();}
else {Ajax=new ActiveXObject("Microsoft.XMLHTTP");}
Ajax.open("GET",url,false);Ajax.send(null);
var newdiv = document.createElement("span");
newdiv.innerHTML = Ajax.responseText;
var container = document.getElementById("container");
container.appendChild(newdiv);
}
</script>

BODY:
<div id="container"></div>
<script type="text/javascript">AjaxInclude("some_file.html")</script>

and this one works in non-IE, and MAY WORK in IE provided we transform the external javascript of the file_to_be_included into internal javascript:

HEAD:
<script type="text/javascript">
AjaxInclude = function (url) {
if (window.XMLHttpRequest) {Ajax=new XMLHttpRequest();}
else {Ajax=new ActiveXObject("Microsoft.XMLHTTP");}
Ajax.open("GET",url,false);Ajax.send(null);
document.write(Ajax.responseText);
}
</script>

BODY:
<script type="text/javascript">AjaxInclude("some_file.html")</script>
But the above scripts don't work in IE or may cause it to crash if we apply them to the Anylink menu (http://www.dynamicdrive.com/dynamicindex1/anylinkcss.htm).
One of the reasons why Ajax-includes using document.write won't always work in IE is given here (http://www.dynamicdrive.com/forums/blog.php?b=29).

Conclusion: Ajax (on the client) is not a good tool for including external content. Things may go wrong in too many cases. This is also true if we replace the simple Ajax scripts given above by more sophisticated ones.
So we should use SSI for including external content. If your server won't allow you to use it, you may perhaps try this (http://www.dynamicdrive.com/forums/blog.php?bt=93) or this (http://www.dynamicdrive.com/forums/blog.php?b=16). The latter method is to be preferred because the 'includer' and the 'included' are in separate windows (if a file_to_be_included has its own window, insertion will always succeed). If the included file isn't a menu, you could also use this (http://www.dynamicdrive.com/forums/showthread.php?t=35844), where, again, 'includer' and 'included' are located in separate windows.
===
Arie Molendijk.










privacy (GDPR)