开发者

Determine whether AJAX is possible?

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 开发者_JS百科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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜