SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting AJAX to work in other browsers

    hi all,

    a development company did some work for our intranet site and this is the folllowing code that they've used to do some AJAX stuff:

    HTML Code:
    function doHTTP(sSrc, destinationElement, params, replaceOuterHTML)
    {
    	var objHTTP, strResult;
    	objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
    	objHTTP.Open('POST', sSrc, false);
    	objHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	objHTTP.send("de="+destinationElement+"&"+params);
    	strResult=objHTTP.responseText;
    	myObject = eval("document.all."+destinationElement);
    	if (!replaceOuterHTML)
    	{
    		if (myObject) myObject.innerHTML = strResult;
    	}
    	else
    	{
    		if (myObject) myObject.outerHTML = strResult;
    	}
    }
    as you will notice that this script will only work in IE - how can i get it to work in other browsers (esp FF)?

    your help would be appreciated.

    many thanks.

  2. #2
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can help a little.

    The IE-only
    Code:
     objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
    becomes:

    Code:
      objHTTP = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    The quite-frankly stone-aged technique:
    Code:
     myObject = eval("document.all."+destinationElement);
    becomes:
    Code:
     myObject = document.getElementById(destinationElement);
    Which will help. I have never used outerHTML though, and I don't know its status when it comes to cross-browser code.

    You also have a problem here in that this is a synchronous AJAX call - and that's really not what I'd recommend. Personally, I'd fire the outside company and redo it myself - but I'm guessing that might not be up to you...
    Only dead fish go with the flow

  3. #3
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the speedy reply - much appreciated

    i'm now getting the following error with FF:

    objHTTP.Open is not a function
    Line: 148

    which is:

    HTML Code:
    objHTTP.Open('POST', sSrc, false);
    i might have a look around for an alternative script for this.

    with regards to the outside company - we aren't using them anymore (thus why i've made it my responsibility to sort this problem out).

  4. #4
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make the 'open' lower-case. Sorry - should have spotted that.
    Only dead fish go with the flow

  5. #5
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    superb! just so that i know for future reference; why did changing 'Open' to 'open' work in FF?

  6. #6
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript is strictly case-sensitive - so you can define an 'open()' function and an 'Open()' function, and they won't clash. Not that that's a particularly good idea What I hadn't realised was that the open call would with in IE even if you capitalised it (I'm guessing it's because it's called on an ActiveX object - and they tend to play by slightly different rules) - anyway - lowercase 'open()' is the correct call for an XMLHttpRequest object.
    Only dead fish go with the flow

  7. #7
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help with this - much appreciated!

  8. #8
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're very welcome.
    Only dead fish go with the flow


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
  •