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 Get IE Version Number
- 5 Get NN (Netscape Navigator) Version Number
- 6 Hiding Scripts from Older Browsers
- 7 JavaScript 1.2 is supported, extract a new URL from the portion
- 8 Multiple Script Versions
- 9 Rendering Different Content for Scriptable and Nonscriptable Browsers
- 10 Set a variable to determine what version of JavaScript we support
- 11 Testing Different JavaScript Versions
- 12 Using Multiple Versions of JavaScript with the language Attribute
- 13 Using the "noscript" Tag
- 14 Variable for Browser version and vendor
A simple "sniffer" that determines browser version and vendor
/*
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;
Check whether JavaScript 1.2 is supported
/*
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
*/
<!-- Check whether JavaScript 1.2 is supported. -->
<script language="JavaScript1.2">var _js12_ = 1.2</script>
<!-- Now avoid the problems with JavaScript 1.2 on Netscape by running -->
<!-- the following code on any browser that supports JavaScript 1.1. If -->
<!-- the browser does not support JavaScript 1.2, however, we"ll display -->
<!-- an error message and suppress any syntax errors that occur. -->
<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>
Employing the "NOSCRIPT" Tag
<HTML>
<HEAD>
<TITLE>Some Document</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// alert("hi");
</SCRIPT>
<NOSCRIPT>
<B>Your browser has JavaScript turned off.</B><BR>
<HR>
</NOSCRIPT>
</HEAD>
<BODY>
<H2>The body of your document.</H2>
</BODY>
</HTML>
Get IE Version Number
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
}
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
}
Hiding Scripts from Older Browsers
<html>
<head>
<title>Hide From Browser</title>
<script type="text/javascript">
<!-- Hide
document.write("I can view JavaScript code")
// End hide -->
</head>
<body>
Content goes here.
</body>
</html>
JavaScript 1.2 is supported, extract a new URL from the portion
/*
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>
<hr size="4">
<h1>This Page Requires JavaScript 1.2</h1>
Your browser cannot run this page. Please upgrade to a browser that
supports JavaScript 1.2, such as Netscape 4 or Internet
Explorer 4.
<hr size="4">
</body>
</html>
Multiple Script Versions
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function doIt() {
// statements for JavaScript 1.0 browsers
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.1">
<!--
function doIt() {
// statements for JavaScript 1.1 browsers
}
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
function doIt() {
// statements for JavaScript 1.2 browsers
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE=button VALUE="Click Me" onClick="doIt()">
</FORM>
</BODY>
</HTML>
Rendering Different Content for Scriptable and Nonscriptable Browsers
<HTML>
<BODY BGCOLOR="#FFFFFF">
<A HREF="http://home.netscape.ru">
<SCRIPT LANGUAGE="JavaScript">
<!--
document.writeln("<A HREF="http://www.wbex.ru">")
//-->
</SCRIPT>
Where?</A>
<HR>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("Howdy from the script!<FONT COLOR="#FFFFFF">")
//-->
</SCRIPT>
If you can read this, JavaScript is not available.
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("</FONT>")
//-->
</SCRIPT>
<BR>
Here"s some stuff afterward.
</BODY>
</HTML>
Set a variable to determine what version of JavaScript we support
/*
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>
<!-- Set a variable to determine what version of JavaScript we support. -->
<!-- This technique can be extended to any number of language versions. -->
<script language="JavaScript"> var _version = 1.0; </script>
<script language="JavaScript1.1"> _version = 1.1; </script>
<script language="JavaScript1.2"> _version = 1.2; </script>
<!-- Run this code on any JavaScript-enabled browser. -->
<!-- If the version is not high enough, display a message. -->
<body>
<script language="JavaScript">
if (_version < 1.1) {
document.write("<hr><h1>This Page Requires JavaScript 1.1</h1>");
document.write("Your JavaScript 1.0 browser cannot run this page.<hr>");
}
</script>
</body>
<!-- Now run the actual program only on JavaScript 1.1 browsers. -->
<script language="JavaScript1.1">
// The actual JavaScript 1.1 code goes here.
</script>
</html>
Testing Different JavaScript Versions
<html>
<head>
<title>JavaScript Unleashed</title>
</head>
<body>
<script language="JavaScript">
<!-- var x = "3";
var y = 7;
document.write("x = "3"");
document.write("<br>");
document.write("y = 7");
document.write("<br><hr>");
document.write("JavaScript 1.0 and 1.1<br>");
document.write("x == 3 : ");
document.write(x == 3);
document.write("<br>");
document.write("y == "7" : ");
document.write(y == "7");
//-->
</script>
<script language="JavaScript1.2">
<!--
document.write("<br><br>JavaScript version 1.2<br>");
document.write("x == 3 : ");
document.write(x == 3);
document.write("<br>");
document.write("y == "7" : ");
document.write(y == "7");
//-->
</script>
<script language="JavaScript1.3">
<!--
document.write("<br><br>JavaScript version 1.3 and higher<br>");
document.write("x == 3 : ");
document.write(x == 3);
document.write("<br>");
document.write("y == "7" : ");
document.write(y == "7");
//-->
</script>
<script language="JavaScript">
<!--
document.write("<br><br>Accomodate all versions.<br>");
document.write("x - 0 == 3 : ");
document.write(x - 0 == 3);
document.write("<br>");
document.write(""" + y == "7" : ");
document.write("" + y == "7"); //-->
</script>
</body>
</html>
Using Multiple Versions of JavaScript with the language Attribute
<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<br>");
</script>
<script language="JavaScript1.1">
//Only JavaScript 1.1 browsers read in this section
document.write("This browser supports JavaScript 1.1<br>");
</script>
<script language="JavaScript1.2">
//Only JavaScript 1.2 browsers read in this section
document.write("This browser supports JavaScript 1.2<br>");
</script>
<script language="JavaScript1.3">
//Only JavaScript 1.3 browsers read in this section
document.write("This browser supports JavaScript 1.3<br>");
</script>
<script language="JavaScript1.4">
//Only JavaScript 1.4 browsers read in this section
document.write("This browser supports JavaScript 1.4<br>");
</script>
<script language="JavaScript1.5">
//Only JavaScript 1.5 browsers read in this section
document.write("This browser supports JavaScript 1.5<br>");
</script>
</body>
</html>
Using the "noscript" Tag
<html>
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>My First JavaScript Script</title>
</head>
<body>
<h1>What is returned?</h1>
<script>
<!--
document.write("<p>JavaScript is turned on!</p>");
//-->
</script>
<noscript>
<p>JavaScript is turned off!</p>
</noscript>
</body>
</html>
Variable for Browser version and vendor
var isNav = (navigator.appName == "Netscape");
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);