When it comes to determining browser support for AJAX, I typically rely on object detection like this:
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
However, more experienced developers often use a try-catch block instead:
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP"); //IE
}
catch(e) // if not IE
{
xhr = new XMLHttpRequest();
}
There are differing opinions on whether try..catch is slower, but ultimately it seems to be a matter of personal preference. Is there a generally accepted approach or convention for handling this? I've encountered similar dilemmas in the past, such as choosing between innerHTML (not standard) and DOM (standard). Thank you for your insights and recommendations.