Wednesday, February 05, 2003

JavaScript Object Detection - WebReference.com (1/3)
Object detection is the practice of finding an object that is supported by a specific browser and then using that object to form the basis of a conditional statement. For example, if we wanted to code only for Internet Explorer we could use the document.all collection to separate Internet Explorer from other browsers.

if (document.all) {
// do something funky here
}

One of the advantages of object detection is that we surpass the hurdle of a browser hiding their identity completely, which can make the detection of user agent strings problematic.

Despite the obvious advantages of object detection, Web developers and designers alike all too frequently make the mistake of not correctly separating browsers through their object detection routines.

For example consider the following simple function:

function whatBrowser() {
if (document.all) {
alert('just a test')
}
}
The alert will trigger correctly in Internet Explorer 4 and above as we would expect. However, the alert will also trigger in Opera based browsers because Opera also supports the document.all collection. Clearly, this is often undesirable as we may be using code specific Internet Explorer features that are not supported in Opera; the consequence of which often results in a badly broken page in Opera.

To complicate matters, the problem isn’t just specific to Opera and Internet Explorer based browsers.…
http://www.webreference.com/programming/javascript/detection/

No comments:

Post a Comment

con·cept