How to find the Javascript version (via code)?

Asked

Viewed 3,527 times

12

Is there any simple way [or not] to find out which version of Javascript is being used in a particular browser - by the code itself written in Javascript?

Something like:

console.log(javascript.version);

This could be useful if some feature was missing in an earlier version.

Example:

if (javascript.version < 1.5) {
   throw new Error("Método 'bind' não existe nessa versão');
}
  • 1

    Just out of curiosity, what’s the point?

  • Know the javascript version :D

  • In fact, it might be useful if some feature was missing in an earlier version

  • See if this helps: http://stackoverflow.com/questions/7340726/detect-version-of-javascript (útlima resposta)

  • Could someone explain why Downvote? When I give Downvote, I do it with purpose!

  • Some people do it for pure luxury. Some questions are good, go downvote people and do not give an opinion for the reason why voted this way, leaving the person with the doubt and no answer.

Show 1 more comment

3 answers

15


Not there is a reliable way to discover the Javascript version.

The use of <script language="Javascript1.*"> has not precision, an example of problem is that in some browsers you can use an attribute language="Javascript1.7" but still you can use only supported functions in language="Javascript1.9".

Another problem is in the Internetexplorer9 for example, it supports various features that should already be at least Javascript1.5, but he always presents Javascript1.3.

The same is true of Firefox, which always displays the result of Javascript1.5.

As stated in other answers there is no function to detect the version of the Javascript engine, note that each browser has a javascript engine own and often such version does not represent the functionalities it actually possesses.

If you rely on such data you are able to disable useful and supported functions that logic should not support in such version of Javascript.

This is because in fact since the beginning of the browser battle, when microsoft launched its own javascript engine (called jscript) and Netscape released his, with many different characteristics (as amazing as it seems the Internetexplorer at that time was much better at it than the competitors).

There was no pattern and only with the emergence of Ecmascript we now have a standard (the development of this specification began in November 1996 and was adopted by Ecma General Assembly in June 1997, according to the pdf document in the cited link).

I believe that today we are in Ecmascript 7, but it does not mean that browsers support all features.

Look at it this way, imagine that we have two technologies of Ecmascript 6 (it’s just an imaginary example):

  • window.minhaFuncaoHelloWordEcma6
  • window.minhaFuncaoFooEcma6

Chorme launched the function window.minhaFuncaoHelloWordEcma6, but can’t stand window.minhaFuncaoFooEcma6, then the browser cannot be Ecmascript6 nor Ecmascript5 because in 5 there is no window.minhaFuncaoHelloWordEcma6 and to be the 6 is necessary window.minhaFuncaoFooEcma6.

So identifying the Javascript or Ecmascript version will never be possible, unless all browsers strictly follow the standard and only support new methods when they all cease to be experimental (or that it is unviable and would make the update process much slower).

Note that today we use many experimental features, so do not use:

<script language="Javascript1.9">
    jsver = 1.9;
</script>

Because you will never get the expected result in different browsers.

Javascript and Mozilla

As per this comment on Soen:

Google-Chrome support Ecmascript 3.1 and Parts of Ecmascript 5. Javascript 1.7 is a Mozilla Creation. No other browsers support Javascript > 1.5, and Instead support Varying versions of Ecmascript. See Relationship between Javascript and Ecmascript version Numbers - Mike Samuel

And the @bfavareto comment:

Don’t you want to add that these "1.x" versions are only valid for Mozilla browsers? That is who has the trademark copyright Javascript, by the way. - bfavaretto

This free program was originally developed by Brendan Eich of Netscape under the name of Mocha, later changed its name to Livescript and finally Javascript.

As a consequence, Microsoft has developed a dialect compatible with the Jscript name language to avoid trademark problems, in other words the terms Javascript is was registered by the old Netscape (today Mozilla - I’m talking about the company).

So Javascript1.7 is a Mozilla creation and so most browsers "non Mozilla", will only work until javascript1.3 or javascript1.5, as they have standardized to follow the versions of ES deployments, so what updates today is the Javascript engine (next to the browser).

Source: https://en.wikipedia.org/wiki/JavaScript

Completion

  • Unable to detect Javascript version
  • Even if the browser says that it is using Javascript1.3 it may already be in a much higher supposed version
  • A browser can supposedly use a version of Javascript and present some "Features" which should not exist in such a version.
  • Cannot detect the full compatibility version with Ecmascript versions (unless you create a library with all kinds of possible tests, which seems impossible)

Follow what @Maniero said, use the Feature Detection

Recommending

I recommend that instead of detecting the Javascript or Ecmascript version try to detect if the function is supported, for example:

function minhaFuncao(arg1) {
    if (typeof window.minhaFuncaoFooEcma6 === "function") {
       return window.minhaFuncaoFooEcma6(arg1);
   }

   return false;
}

Note: In another reply of mine I add some examples of how to detect the functionalities in a more "secure way": How to Detect DOM and API Resources

  • 2

    You agree with mine, because I put what he wanted but I said that it does not serve for anything useful. I put an alternative and links showing what must be done. So I agree with yours, except for the part you disagree with the others :P Maybe the most voted one who only gave what he wanted but says nothing that this is not something that useful.

  • @bigown I had linked only to the onosendai and the Soen, did not Linkei your answer, maybe I gave the wrong idea. I will remove such an excerpt from the reply.

  • The disagreement in itself I find pertinent, is that the way it was written it implied that I disagreed with all and mine went in the same direction as yours.

  • It is not recommended to use anything specified in Ecmascript 6 yet, few Features are supported and are not always the same among browsers: https://kangax.github.io/compat-table/es6/

  • [edited] @Dontvotemedown exactly this is what my example suggests, it is not possible to determine neither the version of Javascript nor the version of ES. Because if the browser uses ES5 with some function of ES6 it would no longer be ES5 and ES6. And so I suggested the "Recommending": I recommend that instead of detecting the javascript version or ecmascript try to detect if the function is supported

  • Yes, your answer makes perfect sense. So I gave up. I commented more because of the source I put, for those who do not know can be useful.

  • 1

    Don’t you want to add that these "1.x" versions are only valid for Mozilla browsers? That is who has the trademark copyright Javascript, by the way.

  • 1

    @bfavaretto Truth, thank you I edited the reply :)

Show 3 more comments

6

By code, in general it has no way. There is no function or variable that indicates this. There are techniques for you to find out if it is really necessary. But you will have to take care of it and see little or no advantage. The best I could find was this:

<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 language="Javascript1.7">
  jsver = 1.7;
</script>
<script language="Javascript1.8">
  jsver = 1.8;
</script>
<script language="Javascript1.9">
  jsver = 1.9;
</script>

<script type="text/javascript">
  alert(jsver);
</script>

Important reading. Other


I find it better to detect the browser version than JS. It makes little sense to detect the JS version. So I’ll keep my original answer here.

I found this function in the OS:

navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem, 
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\bOPR\/(\d+)/);
        if(tem!= null) return 'Opera '+tem[1];
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();

I put in the Github for future reference.

I can’t tell if it is the best possible or if it remains 100% valid. I don’t like to trust things like this. But if you don’t have anything better...

In the same question there are other versions. There must be a reason not to use:

BrowserDetect.version

It is one of the answers, but it is voted little. I hope experts can say why not use it.

  • Returned "Firefox 39". This would be the JS or Browser version?

  • This is the version of Browser, I believe he wants the JS.

  • All right, I get what you want. I’m gonna change.

  • 1

    It’s already fixed. Yeah, I think `Browserdetect is specific. The most important answer is not the solution, but rather you know that it does not help you in practice that it is not this information that you should seek for anything normal.

  • Okay. Now justice has been done. The question was not asked just to find out the version, since if I want to know the version, it is for some reason. And the question of knowing browser version to determine resource usage seems to always be used in most cases

  • I would not use "detect the version" because this does not guarantee whether the technology is present for two factors, the first is that there are features that can be disabled by the user and second because there is way to modify the User-Agent impersonating another browser @Wallacemaxters

  • 2

    @Guilhermenascimento that’s right, so I passed both links in the main part of the reply showing that the Feature Detection is what should be used.

  • @bigown understand what you said, but as I marked Wallace with the @ was because of this comment of his: And the question of knowing browser version to determine resource usage seems to always be used in most cases, even with past links it seems to me that his understanding was another.

  • The "understanding" I arrived at was due to the many changes and a bit of haste, I confess (because I am mostly working when these questions arise). My reading was dynamic, but I understood what you wanted to go through. Thank you for demonstrating the Feature Detection.

  • I hope you will not judge me for marking the other answer as resolved, but I believe that in that case it is more convenient :)

  • 3

    Today I’m awarded, it’s one negative after another.

Show 6 more comments

4

Via conditional compilation:

<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 language="Javascript1.7">
  jsver = 1.7;
</script>
<script language="Javascript1.8">
  jsver = 1.8;
</script>
<script language="Javascript1.9">
  jsver = 1.9;
</script>

<script type="text/javascript">
  alert(jsver);
</script>

My current Chrome reports version 1.7.

Sources: #1, #2.

  • 1

    O.o. In mine showed 1.5. I’m using Firefox 39

  • The answer accepted in my recommendation uses that same strategy, so I would go down that path @Onosendai posted.

  • @correct Cantoni - I had not seen your comment. Post as response, so we can vote.

  • No problem @Onosendai. Leave it at that. It’s all right. :-)

  • 1

    @Wallacemaxters I tested here, and I can confirm - 1.5 on FF.

  • 1

    in my FF (39.0.3) also returned 1.5.

  • If you are using PHP, you can make one for not have to repeat code. Or use javascript to include it via document.createelement('script')

  • IE - 1.3. Kind of explains certain behaviors... [Edit] Edge (Windows 10) also reports 1.3.

  • Safari latest version (8.0.7) returns 1.7

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.