Scripting Methods

Object Detection

Checking for the existence of each function prior to using it is the best approach to DOM programming. However, there are times when you may need to provide a simple high-pass (or low-pass) filter to determine the general level of support. This is frequently much simpler than parsing a complex user agent string and much more reliable, since DOM support cannot be faked.

Below is a list of boolean expressions, a list of which browsers will evaluate the expression to true, and the results on your current browser.

Simple Detection Expressions

expression current Gecko Presto WebKit Trident Tazman KHTML Cab Netscape 4
document.layers
window.clientInformation <8
typeof(document.fileCreatedDate)=='string' <7
typeof(document.fileModifiedDate)=='string' <7
document.body
document.body.contentEditable=='inherit' !
typeof(document.media)=='string' 5.5+
window.print >1.1.5
typeof(window.print)=='object'
typeof(window.print)=='function' >1.1.5
document.all
typeof(document.all)=='object'
typeof(document.all)=='function'
document.getElementById
typeof(document.getElementById)=='object'
typeof(document.getElementById)=='function'
document.compatMode=='CSS1Compat' 6+
typeof(document.implementation)=='object' 6+
document.createComment 5.5+
typeof(document.createComment)=='object' 6+
typeof(document.createComment)=='function'
typeof(document.doctype)=='object' 6+
document.createProcessingInstruction 8+
document.doctype!=null

More Information

Scripting MIME Types

The text/javascript type is understood by all major browsers, but is not actually a registered MIME type.

MIME type current Gecko Presto WebKit Trident Tazman KHTML Cab Netscape 4
application/ecmascript 2006+ 9+ <1.2
application/javascript 2006+ 9+ <1.2
application/x-ecmascript 9+ <1.2
application/x-javascript
application/x-notscript
text/ecmascript 2006+
text/javascript
text/javascript1.1
text/javascript1.2
text/jscript
text/livescript
text/perlscript (if installed)
text/vbscript or text/vbs

Note

Internet Explorer only supports text/perlscript when the ActivePerl scripting engine is installed.

Warning

Internet Explorer downloads all scripts and styles, even when the style media (e.g. handheld or braille) or script MIME type (e.g. text/x-notscript) means the content will never be used (this is stupid). Opera 7 downloads text/vbscript and does not use it. This makes it difficult to use server logs to determine browser audience with this info. This also means that IE users will suffer the download cost for this alternate content.

Tip

Only Presto 7.2 and later display noscript content that follows an unsupported script type.

Note

MSN-TV/WebTV and Pocket Internet Explorer ignore the script type, and simply run everything as if it were JavaScript.

Microsoft JScript Conditional Compilation

Microsoft included a simple preprocessor in JScript for conditional compilation. These statements are typically used within comments since this is an extension to typical Javascript/ECMAScript syntax.

Example

/*@cc_on@*/  /* initialize conditional compilation */
/*@set @debug= 2/3 @*/
/*@if(@_win32)
  alert('Windows');
@elif(@_win16)
  alert('ancient Windows');
@else @*/
  alert('not Windows');
/*@end@*/

All @set statement expressions may only use literals, preprocessor variables, and operators. The following boolean (true or NaN) variables are available: @_win32, @_win16, @_mac, @_alpha, @_x86, @_mc680x0, @_PowerPC, @_debug, @_fast, and @_jscript (@_jscript is always true). The @_jscript_build and @_jscript_version variables provide numeric version information.

Warning

The conditional compilation support in the Macintosh version of Internet Explorer is incomplete. Only @_jscript is true. Always use the opposite of @_win32 rather than @_mac.