Get best price on Prepaid Cheapest Smartphone | ||||||||||||
|
||||||||||||
|
JavaScript : Browser VersioningThere are different browsers and versions out there. Thus you, the programmer, do not know what JavaScript capabilities the user has. Insuring that the scripts you write run in most users browsers can be done in a few different ways. One way is to use JavaScript to check which browser they are using, and then program accordingly. This is the most common way to avoid any scripting errors on the different browsers. The information at the top of the page was printed out using the following functions, which retrieve the browser version, name and user agent:
<SCRIPT LANGUAGE="JavaScript">
document.writeln(parseInt(navigator.appVersion)); document.writeln(navigator.appName); document.writeln(navigator.userAgent); </SCRIPT> The (Netscape) Navigator name is part of the function because Netscape created JavaScript. Here are some examples have how these can be used:
<SCRIPT LANGUAGE="JavaScript"><!--
var iVer = parseInt(navigator.appVersion); var sName = navigator.appName; if (iVer >= 3) { ...perform some function... } if (sName == "Netscape") { ... do this ... } else if (sName == "Microsoft Internet Explorer") { ...do something else... } //--> </SCRIPT> You can also use this when calling functions within the body part of the document. For example:
<FORM>
<INPUT TYPE=button onClick="if (iVer >= 4) doThisFunction();"> </FORM> However, the above way requires you to know which specific functions and methods are supported by which browser, and which version. The following two ways try to get around that. A way to try to version-detect your scripts is to use the actual script tag. You can specify the version in the LANGUAGE value. The browsers which support the level of JavaScript specified will run the script, if not supported it should not run it. This feature is only supported by Netscape. Examples:
<SCRIPT LANGUAGE="JavaScript1.0">
<SCRIPT LANGUAGE="JavaScript1.1">
<SCRIPT LANGUAGE="JavaScript1.2">
Netscape ver 2.02 supports JS ver 1.0, Netscape 3.0x supports ver 1.1, and you could probably guess that Netscape 4.0x supports JavaScript ver 1.2. However, that way requires you to know which functions came out in what version, and it only works on Netscape, which is not good enough. Another way to try to assure your scripts run fine, is not to check what browser is used, but to check if the JavaScript object is defined. You can do this with a simple if statement. For example, in 2.0 browsers, images are not defined as objects in JavaScript, thus the JavaScript object document.images is not defined. Undefined is considered to be false, such as in an if statement. if (document.images) { ...document.images is defined }
That is all there is to it. The drawback to using this method is that you may have several if statements checking several different objects at once. It is always best to check your code in numerous different browsers, with numerous different settings.
Related Links: JavaScript 1.1 Reference (Navigator 3.x. Although out of date, the links to newer docs that Netscape recommends do not work. Still, lots of valid reference info.) Microsoft JScript (Internet Explorer) ECMA Script Specification (3rd edition, 12/99)
|
privacy policy || © 1997-2016. astonishinc.com All Rights Reserved. |