开发者

To tell Javascript version of your browser

I have the following script from http://javascript.about.com/library/bljver.htm

<script type="text/javascript">
var jsver = 1.0;
</script>
<script language="Javascript1.1">
jsver = 1.1;
</script>
<script开发者_运维问答 language="Javascript1.2">
jsver = 1.2;
</script>
<script language="Javascript1.3">
jsver = 1.3;
</script>
<script language="Javascript1.4">
jsver = 1.4;
</script>
<script language="Javascript1.5">
jsver = 1.5;
</script>
<script language="Javascript1.6">
jsver = 1.6;
</script>
<script type="text/javascript">
document.write('<p><b>Javascript version ' + jsver + ' supported<\/b><\/p>');
</script>

but wonder if there is a shorter way?


Looking at the comment you made to @meder, I would highly recommend you to go for feature detection, for example, to detect if the indexOf method is available on Array objects:

if (typeof Array.prototype.indexOf == 'function') {
  //...
}

The JavaScript (TM) version numbers refer to the Mozilla implementation of the ECMAScript Standard.

In the early years, when the language attribute was widely used, you could for example specify "JavaScript1.2" to place code that was written for ECMAScript 2, but I wouldn't recommend you this approach nowadays.

The only reason you might want to specify a JavaScript(TM) version, is because you really need to use a Mozilla-specific extension, for example the let keyword, generators, iterators, expression closures, etc...

See also:

  • Which Javascript version(s) does IE8 supports?


In the real world, Javascript has no meaningful version numbers, especially in IE.

This isn't really possible.


Javascript doesn't really have different version numbers, and even when it does, the versions aren't particularly useful. The best way is to do feature detection. There are several projects and libraries which extend native types with features like Array.indexOf, Object.keys, addEventListener, etc. Like http://devpro.it/JSL/ and https://code.google.com/p/ddr-ecma5/


You shouldn't use the language attribute, it's oldschool and not really relevant in this day and age. In addition, there is no standard for relaying the version of Javascript cross-browser. Not to mention different browsers have inconsistent Javascript versions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜