Detecting Browser Capabilities

Although browsers support most JScript features, the new features that target the .NET Framework, class-based objects, data types, enumerations, conditional compilation directives, and the const statement, are supported only on the server-side. Consequently, you should use these features exclusively in server-side scripts. For more information, see JScript Version Information.

A JScript script can detect the capabilities of the engine that interprets or compiles it. This is unnecessary if you are writing code for a server-side application (to be run in ASP or ASP.NET) or a command-line program since you can easily discover the supported JScript version and code accordingly. However, when running client-side scripts in a browser, this detection is important to ensure that the script is compatible with the JScript engine in the browser.

There are two ways to check for JScript compatibility, either by using the script engine functions or by using conditional compilation. There are advantages to using both approaches.

Script Engine Functions

The script engine functions (ScriptEngine, ScriptEngineBuildVersion, ScriptEngineMajorVersion, ScriptEngineMinorVersion) return information about the current version of the script engine. For more information, see Functions (Visual Studio - JScript).

For the most compatibility, only features found in JScript Version 1 should be used in a page that checks supported JScript versions. If an engine supports a version of JScript higher than 1.0, you can redirect to another page that includes the advanced features. This means that you must have a separate version of each Web page corresponding to each version of JScript you want to support. In most situations, the most efficient solution is to have only two pages, one designed for a particular version of JScript, and the other designed to work without JScript.

Note

JScript code that uses advanced features must be placed in a separate page that is not run by browsers with incompatible engines. This is mandatory because the script engine of a browser interprets all the JScript code contained in a page. Using an if…else statement to switch between a block of code that uses the latest version of JScript and a block of JScript version 1 code will not work for older engines.

The following example illustrates the use of the script engine functions. Since these functions were introduced in JScript Version 2.0, you must first determine if the engine supports the functions before attempting to use them. If the engine supports only JScript Version 1.0 or does not recognize JScript, the typeof operator will return the string "undefined" for each function name.

if("undefined" == typeof ScriptEngine) {
   // This code is run if the script engine does not support
   // the script engine functions.
   var version = 1;
} else {
   var version = ScriptEngineMajorVersion();
}
// Display the version of the script engine.
alert("Engine supports JScript version " + version);
// Use the version information to choose a page.
if(version >= 5) {
   // Send engines compatible with JScript 5.0 and better to one page.
   var newPage = "webpageV5.htm";
} else {
   // Send engines that do not interpret JScript 5.0 to another page.
   var newPage = "webpagePre5.htm";
}
location.replace(newPage);

Conditional Compilation

Conditional compilation variables and statements can hide JScript code from engines that do not support conditional compilation. This approach is useful if you want to include a small amount of alternate code directly in the Web page.

Note

Do not use multiline comments within the conditional compilation blocks since engines that do not support conditional compilation may misinterpret them.

<script>
/*@cc_on
@if(@_jscript_version >= 5 )
// Can use JScript Version 5 features such as the for...in statement.
// Initialize an object with an object literal.
var obj = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"};
var key;
// Iterate the properties.
for (key in obj) {
   document.write("The "+key+" property has value "+obj[key]+".<BR>");
}
@else 
@*/
alert("Engine cannot interpret JScript Version 5 code.");
//@end
</script>

If the conditional @if block includes a lot of code, it may be easier to use the approach outlined above for using the script engine functions.

See Also

Concepts

JScript Version Information

Other Resources

Writing, Compiling, and Debugging JScript Code

Functions (Visual Studio - JScript)

Conditional Compilation