Results 1 to 1 of 1

Thread: Alternative to AJAX

  1. #1
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default 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):
    Code:
    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:
    Code:
    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.
    One of the reasons why Ajax-includes using document.write won't always work in IE is given here.

    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 or this. 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, where, again, 'includer' and 'included' are located in separate windows.
    ===
    Arie Molendijk.
    Last edited by molendijk; 05-22-2009 at 08:18 AM. Reason: Correction

Bookmarks

Posting Permissions

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