Browser, Schmouser(the diverge between JScript and JavaScript)
Posted on 2006-02-24 00:52 moris 阅读(164) 评论(0) 收藏 举报by Heidi Brumbaugh
From:http://www.webdeveloper.com/javascript/javascript_browser_checking.html
I've been thinking a lot about my six-year old lately, who's been having trouble making friends since our recent move. The problem is that she loves having other kids over, as long as they play her games. If they don't she pouts, then agrees to play what they're playing, but then immediately starts in to make her own arbitrary and dictatorial changes to the rules.
Which brings us to the topic of this article: Microsoft's implementation of JavaScript.
I should scribble that technically, since Internet Explorer does not support JavaScript per se(拉丁语,意思是"本身,本质上"), but instead supports a scripting protocol that looks a lot like JavaScript, is called JScript, and gives it its best shot when it encounters a 〈SCRIPT language = "JavaScript"〉 tag. Here's the official description, pulled off Microsoft's Web site: "Microsoft JScript is an open implementation of Netscape's JavaScript".
To make matters even more confusing, each browser version contains its own "upgrades" to the protocol, giving us a geometric progression of compatibility problems. This article will give you a overview of JScript and discuss some of the more glaring compatibility issues. Then it will give code to check which browser (and version) your user is running. Finally, I'll point to some resources to give you more thorough details on the incompatibilities.
There are two aspects to JavaScript/JScript, and thereby two opportunities for the implementations to diverge. The first aspect is the language itself (the keywords that tell the interpreter to do this, compare this and that, then do this, but only if that). Version 3.0 of JScript, supported in Internet Explore 4.0, has expanded this language substantially. For example, Microsoft added a switch statement, which lets you easily choose among a number of options. You can now label statements and then jump to those statements with break and continue, giving you finer control over your loops. Handily (coincidentally? ironically?), one of the new features is conditional compilation, which makes it easier to write code which will only run under specific browsers.
The second aspect to JavaScript/JScript is the set of specific objects, properties, and methods associated with the browser and document. It is the definition of this core set that defines the reach the script has over the HTML document. To understand where Microsoft is coming from, you need to keep in mind that the object model was defined not for JScript, but for the progression of Microsoft's ActiveX technology onto the Internet. Know also that Microsoft already had its own scripting language, VBScript, which is implemented in its major products and into which Microsoft is heavily invested. From this perspective, the addition of JScript seems no more than a grudging afterthought, included probably with the idea that compatibility would give IE a competetive advantage over Netscape Navigator.
One key difference in the JScript object model is that there is no Area or Image object. Because of this, you can't use JScript directly to change an image on an HTML page. As more than one reader pointed out to me after another article, the onMouseOver example simply won't work in Internet Explorer. Sigh.
Once you're aware of the compatibility problems between the two major browsers, the next step is to manage the problems as gracefully as possible. The most effective technique is to add a statement checking the name and version number of the user's browser, and then execute code only where it's supported.
Browser information is stored in the navigator object. The appName property specifies the name of the browser and appVersion specifies the version. So to execute different code depending on the browser name, you would use an if statement, as in:
Run this program to get a closer look at the properties of the navigator object.
For more details on the differences between JavaScript and JScript, Gordon McComb's JavaScript Sourcebook site has good info. To go to the horse's mouth, visit Microsoft's JScript site. Another good resource is the Object Model document. And don't forget the old faithful Netscape references.
Thanks for all the email! This article is a direct result of readers' questions. Write to me at heidib@well.com, and remember, I need your feedback but I can't guarantee an individual response.
Heidi Brumbaugh has been a writer and editor in the computer publishing industry for ten years. Send her your comments or questions about this article and JavaScript. While we can't guarantee an individual response, questions of general interest may be addressed in a future article. Feel free to visit her home page.
if (navigator.appName == "Netscape")
document.write("Netscape Navigator rules!! All right!!!")
else
document.write("Internet Explorer rules!! Way to go!!!")
浙公网安备 33010602011771号