Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax Browser Compatibility (2) 1

Status
Not open for further replies.

snowboardr

Programmer
Joined
Feb 22, 2002
Messages
1,401
Location
PH
I am somewhat new to Ajax, so bare with.. I need to modify the code below to work with opera 8 etc.. and it doesnt seem to be working in IE... I get the error "Error. Scripting for ActiveX might be disabled" but IE isnt disabled. The ajax works fine as far as saving, in Firefox.. etc

-----------------------------------------
I do realise I have posted this twice, however you can see on thread216-1203332 it didn't exactly get answered
-----------------------------------------


Code:
var xmlHttp

function SaveCollapse(intCall,SaveThis)
{
var url="ajax_save.asp?call=" + intCall + "&save=" + SaveThis
xmlHttp=GetXmlHttpObject()
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
}




function GetXmlHttpObject(handler)
{
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This example doesn't work in Opera")
return;
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
}
try
{
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler
return objXmlHttp
}
catch(e)
{
alert("Error. Scripting for ActiveX might be disabled")
return
}
}
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler
return objXmlHttp
}
}

 
>[tt]xmlHttp=GetXmlHttpObject[highlight]()[/highlight][/tt]
>[tt]function GetXmlHttpObject[highlight](handler)[/highlight][/tt]
>[tt]objXmlHttp.onreadystatechange=handler[/tt]
>[tt]objXmlHttp.onerror=handler[/tt]

It seems you cannot assign an undefined handler in ie, whereas moz let pass.

 
ok i went with this then:

Code:
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
 
doesn't seem to work in opera, does anyone know a fix for opera 8 +

- regards
jason
 
Opera is strong in css support. xml support? I wouldn't worry about it too much for the moment.
 
I have been using AJAX with Opera 8 for ages now. There is certainly no need to make up reasons to be lazy! Here is the code I use to instantiate the object:
Code:
function createRequestObject() {
	var ro;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				ro = false;
			}
		}
	@else
		ro = false;
	@end @*/
	if (!ro && typeof XMLHttpRequest != 'undefined') {
		try {
			ro = new XMLHttpRequest();
		} catch (e) {
			ro = false;
		}
	}
	return ro;
}

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
That works swell Jeff! Many thanks :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top