Browser Compatibility: Method Detection - Doc JavaScript
Method Detection
Object detection is great, but JavaScript is more than just objects and properties -- it features functions and methods as well. Suppose you want to define a regular expression, but regular expressions are only supported in fourth-generation browsers. The easiest and most reliable way to check if the user's browser supports regular expressions is to find out if it supports the RegExp()
constructor function. In order to check if this function exists, you must use a function reference, as opposed to a function call (e.g., RegExp
rather than RegExp()
). Secondly, you must specify its parent, which is the top-level window
object. So the following statement determines whether the user's browser supports regular expressions:
if (window.RegExp) {
// regular expression statements here
} else {
// a workaround without regular expressions here
}
* This object detection routine does not work with Internet Explorer 4.0 PR1 (Preview Release 1).
Note that the parent of all functions is the window
object, so you would use window.setInterval
to check if the setInterval()
function is supported.
When dealing with methods you should obviosly specify the method's full path, including its parent object. Here's an example:
if (top.frames[0].document.captureEvents) {
// additional statements here
}
Created: November 4, 1997
Revised: December 4, 1997
URL: https://www.webreference.com/js/column6/method.html