views:

89

answers:

2

I've a scenario where a client's internet explorer 6 browser does not allow instantances of activex controls to be created rendering ajax inoperable in jquery.

This also pops a warning at the top of the user's browser.

Is there anyway to detect that ActiveX is available without generating any warnings for the user? I'd like to use ajax if it's available but I would like to have the function degrade to a classic form post if the object cannot be created.

+8  A: 
var xhr;
if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    try {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(err) { 

    }
}
else {
    //It couldn't find any of those Ajax objects
}

if(!xhr) {
    //Here you know now that Ajax is not possible with this client
}

This is a simple implementation. There are better ones out there though.

Update:

Try...Catch suggested by JasonWyatt. I missed that.

Colour Blend
Might want to put a try/catch around the call to "new ActiveXObject" in case it's an exception that is thrown when JS tries to create such an object...
JasonWyatt
appears !!window.ActiveXObject is true when i manually turn of activex in the security settings
Dave
+1 for the tip.
Colour Blend
The comment is not quite correct...if an error is thrown, the "else" block won't execute. Remove the else, replace with if(!xhr){}, which will be a proper check to see if the xhr object was created successfully or not.
jvenema
@Jvenema: Thanks.
Colour Blend
A: 

Note: You might want to have a fall-back which does not require javascript at all, using the <NoScript> tag.

Also, this reminded me of a bad example from The Daily WTF.

Johan
i use jquery to hide all the manual form posting stuff, if they don't have javascript enabled then the original form is still intact
Dave