Forum Moderators: open

Message Too Old, No Replies

Browser compatibility with AJAX

Temperamental Code

         

trillianjedi

10:43 am on Jun 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got a strange problem here. I actually have a book on AJAX (remember books? :) ) but I think it might be a little out of date.

I can't seem to get true browser compatibility with this standard bit of AJAX code:-

<script language="javascript">
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");}

function getData(dataSource, divID)
{
if (XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource);

XMLHttpRequestObject.onreadystatechange = function ()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}

XMLHttpRequestObject.send(null);
}
}

The latest IE "fix" issued April 2006 seems to break it, and it doesn't seem to work in FireFox all the time, depending on how up to date your install of FF is.

Is there a "new" standard for setting up the XMLHttpRequestObject which is likely to give me more consistent results in light of the April IE patch and latest FF patches?

Thanks.

TJ

Little_G

11:50 am on Jun 10, 2006 (gmt 0)

10+ Year Member



Hi,

Take at look at the code here [developer.mozilla.org], there is a second name for the XMLHttpRequestObject in Internet Explorer.

Andrew

TomAnthony

3:25 am on Jun 11, 2006 (gmt 0)

10+ Year Member



Is Msxml2.XMLHTTP newer than Microsoft.XMLHTTP? I'm not sure, never have worked it out.

Here is the code I use for creating the request object, not sure if it will help you, but should only take you a second to try it. Let us know either way. :)


var ajax_connection = createRequest();


function createRequest()
{
var ajaxRequest;


try
{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1)
{
try
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
ajaxRequest = new XMLHttpRequest();
}
}


return ajaxRequest;
}

trillianjedi

6:06 am on Jun 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys - yes, looks like I was missing one of the alternatives. I'm not sure which code is better - all three seem to work now actually.

TJ