JavaScript DHTML/Development/JavaScript Version
Содержание
- 1 A simple "sniffer" that determines browser version and vendor
- 2 Check whether JavaScript 1.2 is supported
- 3 Employing the "NOSCRIPT" Tag
- 4 The body of your document.
- 5 Get IE Version Number
- 6 Get NN (Netscape Navigator) Version Number
- 7 Hiding Scripts from Older Browsers
- 8 JavaScript 1.2 is supported, extract a new URL from the portion
- 9 This Page Requires JavaScript 1.2
- 10 This Page Requires JavaScript 1.1
- 11 What is returned?
A simple "sniffer" that determines browser version and vendor
<source lang="html4strict">
/* Examples From JavaScript: The Definitive Guide, Fourth Edition Legal matters: these files were created by David Flanagan, and are Copyright (c) 2001 by David Flanagan. You may use, study, modify, and distribute them for any purpose. Please note that these examples are provided "as-is" and come with no warranty of any kind. David Flanagan
- /
/*
* File: browser.js * Include with: <script SRC="browser.js"></script> * * A simple "sniffer" that determines browser version and vendor. * It creates an object named "browser" that is easier to use than * the "navigator" object. */
// Create the browser object. var browser = new Object(); // Figure out the browser major version. browser.version = parseInt(navigator.appVersion); // Now figure out if the browser is from one of the two // major browser vendors. Start by assuming it is not. browser.isNetscape = false; browser.isMicrosoft = false; if (navigator.appName.indexOf("Netscape") != -1)
browser.isNetscape = true;
else if (navigator.appName.indexOf("Microsoft") != -1)
browser.isMicrosoft = true;
</source>
Check whether JavaScript 1.2 is supported
<source lang="html4strict">
/* Examples From JavaScript: The Definitive Guide, Fourth Edition Legal matters: these files were created by David Flanagan, and are Copyright (c) 2001 by David Flanagan. You may use, study, modify, and distribute them for any purpose. Please note that these examples are provided "as-is" and come with no warranty of any kind. David Flanagan
- /
<script language="JavaScript1.2">var _js12_ = 1.2</script>
<script language="JavaScript1.1">
// If JavaScript 1.2 is not supported, fail gracefully.
function supressErrors() { return true; }
if (!_js12_) {
window.onerror = supressErrors; alert("This program requires a browser with JavaScript 1.2 support");
} // Now proceed with the JavaScript 1.2 code. </script>
</source>
Employing the "NOSCRIPT" Tag
<source lang="html4strict">
<HTML> <HEAD> <TITLE>Some Document</TITLE> <SCRIPT LANGUAGE="JavaScript">
// alert("hi");
</SCRIPT>
<NOSCRIPT>
Your browser has JavaScript turned off.
</NOSCRIPT> </HEAD> <BODY>
The body of your document.
</BODY> </HTML>
</source>
Get IE Version Number
<source lang="html4strict">
function getIEVersionNumber() {
var ua = navigator.userAgent; var MSIEOffset = ua.indexOf("MSIE "); if (MSIEOffset == -1) { return 0; } else { return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset))); }
} var isIE5Min = getIEVersionNumber() >= 5; if (isIE5Min) {
// perform statements for IE 5 or later
}
</source>
<source lang="html4strict">
function getNNVersionNumber() {
if (navigator.appName == "Netscape") { var appVer = parseFloat(navigator.appVersion); if (appVer < 5) { return appVer; } else { if (typeof navigator.vendorSub != "undefined") { return parseFloat(navigator.vendorSub); } } } return 0;
} var isNN6Min = getNNVersionNumber() >= 6; if (isNN6Min) {
// perform statements for NN 6 or later
}
</source>
Hiding Scripts from Older Browsers
<source lang="html4strict">
<html> <head>
<title>Hide From Browser</title> <script type="text/javascript">
</head> <body> Content goes here. </body> </html>
</source>
JavaScript 1.2 is supported, extract a new URL from the portion
<source lang="html4strict">
/* Examples From JavaScript: The Definitive Guide, Fourth Edition Legal matters: these files were created by David Flanagan, and are Copyright (c) 2001 by David Flanagan. You may use, study, modify, and distribute them for any purpose. Please note that these examples are provided "as-is" and come with no warranty of any kind. David Flanagan
- /
<html> <head> <script language="JavaScript1.2"> // If JavaScript 1.2 is supported, extract a new URL from the portion of // our URL following the question mark, and load that new URL in. location.replace(location.search.substring(1)); // Enter a really long, empty, loop so that the body of this document // doesn"t get displayed while the new document is loading. for(var i = 0; i < 10000000; i++); </script> </head> <body>
This Page Requires JavaScript 1.2
Your browser cannot run this page. Please upgrade to a browser that supports JavaScript 1.2, such as Netscape 4 or Internet Explorer 4.
</body> </html>
</source>
Multiple Script Versions
<source lang="html4strict">
<HTML> <HEAD>
<SCRIPT LANGUAGE="JavaScript"> </SCRIPT> <SCRIPT LANGUAGE="JavaScript1.1"> </SCRIPT> <SCRIPT LANGUAGE="JavaScript1.2"> </SCRIPT>
</HEAD> <BODY> <FORM>
<INPUT TYPE=button VALUE="Click Me" onClick="doIt()">
</FORM> </BODY> </HTML>
</source>
Rendering Different Content for Scriptable and Nonscriptable Browsers
<source lang="html4strict">
<HTML> <BODY BGCOLOR="#FFFFFF"> <A HREF="http://home.netscape.ru"> <SCRIPT LANGUAGE="JavaScript">
</SCRIPT> Where?</A>
<SCRIPT LANGUAGE="JavaScript">
</SCRIPT> If you can read this, JavaScript is not available. <SCRIPT LANGUAGE="JavaScript">
</SCRIPT>
Here"s some stuff afterward.
</BODY>
</HTML>
</source>
Set a variable to determine what version of JavaScript we support
<source lang="html4strict">
/* Examples From JavaScript: The Definitive Guide, Fourth Edition Legal matters: these files were created by David Flanagan, and are Copyright (c) 2001 by David Flanagan. You may use, study, modify, and distribute them for any purpose. Please note that these examples are provided "as-is" and come with no warranty of any kind. David Flanagan
- /
<html>
<script language="JavaScript"> var _version = 1.0; </script>
<script language="JavaScript1.1"> _version = 1.1; </script>
<script language="JavaScript1.2"> _version = 1.2; </script>
<body>
<script language="JavaScript">
if (_version < 1.1) {document.write("
This Page Requires JavaScript 1.1
"); document.write("Your JavaScript 1.0 browser cannot run this page.");
}
</script> </body>
<script language="JavaScript1.1">
// The actual JavaScript 1.1 code goes here.
</script> </html>
</source>
Testing Different JavaScript Versions
<source lang="html4strict">
<html> <head>
<title>JavaScript Unleashed</title>
</head> <body>
<script language="JavaScript"> </script> <script language="JavaScript1.2"> </script> <script language="JavaScript1.3"> </script> <script language="JavaScript"> </script>
</body> </html>
</source>
Using Multiple Versions of JavaScript with the language Attribute
<source lang="html4strict">
<html> <head>
<title>JavaScript version test page</title>
</head> <body> <script language="JavaScript">
//Only JavaScript 1.0 browsers read in this section document.write("This browser supports JavaScript 1.0
");
</script>
<script language="JavaScript1.1">
//Only JavaScript 1.1 browsers read in this section document.write("This browser supports JavaScript 1.1
");
</script> <script language="JavaScript1.2">
//Only JavaScript 1.2 browsers read in this section document.write("This browser supports JavaScript 1.2
");
</script> <script language="JavaScript1.3">
//Only JavaScript 1.3 browsers read in this section document.write("This browser supports JavaScript 1.3
");
</script> <script language="JavaScript1.4">
//Only JavaScript 1.4 browsers read in this section document.write("This browser supports JavaScript 1.4
");
</script> <script language="JavaScript1.5">
//Only JavaScript 1.5 browsers read in this section document.write("This browser supports JavaScript 1.5
");
</script> </body> </html>
</source>
Using the "noscript" Tag
<source lang="html4strict">
<html> <head>
<meta http-equiv="Content-Script-Type" content="text/javascript"> <title>My First JavaScript Script</title>
</head> <body>
What is returned?
<script> </script> <noscript>
JavaScript is turned off!
</noscript>
</body> </html>
</source>
Variable for Browser version and vendor
<source lang="html4strict">
var isNav = (navigator.appName == "Netscape"); var isIE = (navigator.appName == "Microsoft Internet Explorer"); var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
</source>