views:

63

answers:

4

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?

A: 

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.

meder
to tell why function such as [1,3,5].indexOf(3) is not supported by IE... requires Javascript 1.6, and what IE supports, and just to know about things, like most people like to
動靜能量
this is not really an answer... if you put what you put down here other people looking for unanswered questions may ignore this question.
動靜能量
+1  A: 

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

This isn't really possible.

SLaks
Remember that JavaScript(TM) != JScript
CMS
+5  A: 

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:

CMS
+1  A: 

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/

antimatter15