简体   繁体   English

告诉您浏览器的Javascript版本

[英]To tell Javascript version of your browser

I have the following script from http://javascript.about.com/library/bljver.htm 我有来自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: 查看您对@meder所作的评论,我强烈建议您进行特征检测 ,例如,检测indexOf方法是否可用于Array对象:

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

The JavaScript (TM) version numbers refer to the Mozilla implementation of the ECMAScript Standard. JavaScript(TM)版本号是指ECMAScript标准的Mozilla实现

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. 在最初的几年中,当language属性被广泛使用时,您可以例如指定"JavaScript1.2"来放置为ECMAScript 2编写的代码,但如今我不建议您使用这种方法。

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... 您可能要指定JavaScript™版本的唯一原因是,您确实需要使用Mozilla特定的扩展名例如 let关键字,生成器,迭代器,表达式闭包等。

See also: 也可以看看:

In the real world, Javascript has no meaningful version numbers, especially in IE. 在现实世界中,JavaScript没有有意义的版本号,尤其是在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. Javascript实际上并没有不同的版本号,即使有,版本也不是特别有用。 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/ 有几个项目和库通过Array.indexOf,Object.keys,addEventListener等功能扩展了本机类型。例如http://devpro.it/JSL/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. 您不应该使用language属性,它是老式的,在当今时代并不重要。 In addition, there is no standard for relaying the version of Javascript cross-browser. 另外,没有中继Javascript跨浏览器版本的标准。 Not to mention different browsers have inconsistent Javascript versions. 更不用说其他浏览器的Javascript版本不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM