Detect version of JavaScript

前端 未结 4 1430
终归单人心
终归单人心 2020-11-29 02:35

How can I detect what version of JavaScript the browser supports (if any)? I want to check for ECMAScript 3 versus ECMAScript 5 versus ECMAScript 6.

Note: I want to

相关标签:
4条回答
  • 2020-11-29 03:12

    Here is a good reference for you: http://www.docsteve.com/DocSteve/Samples/JS/js_version.html

    Basically, use the deprecated language attribute of the script tag

    <script language="javascript">var js_version="1.0"</script>
    <script language="javascript1.1">var js_version="1.1"</script>
    <script language="javascript1.2">var js_version="1.2"</script>
    <script language="javascript1.3">var js_version="1.3"</script>
    <script language="javascript1.4">var js_version="1.4"</script>
    <script language="javascript1.5">var js_version="1.5"</script>
    <script language="javascript1.6">var js_version="1.6"</script>
    
    0 讨论(0)
  • 2020-11-29 03:23

    The solution proposed to http://www.docsteve.com/DocSteve/Samples/JS/js_version.html as simple JavaScript function (using "createElement" construction):

    <script type="text/javascript">
    function get_js_version ()
    {
        this.jsv = {
                versions: [
                    "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2.0"
                ],
                version: ""
            };
    
        var d = document;
    
        for (i = 0; i < jsv.versions.length; i++) {
            var g = d.createElement('script'),
                s = d.getElementsByTagName('script')[0];
    
                g.setAttribute("language", "JavaScript" + jsv.versions[i]);
                g.text = "this.jsv.version='" + jsv.versions[i] + "';";
                s.parentNode.insertBefore(g, s);
        }
    
        return jsv.version;
    }
    
    document.write('JavaScript Version: ' + get_js_version());
    </script>
    
    0 讨论(0)
  • 2020-11-29 03:27

    This pops out an alert box with the javascript version being used by your browser:

    <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>

    Related jsfiddle.net

    0 讨论(0)
  • 2020-11-29 03:36

    I suppose it depends on what you want to do with the information, but many people prefer to do feature detection, instead of figuring out what browser someone is using or what version of JS.

    Check out Modernizr, which is a great library that does feature detection for you.

    0 讨论(0)
提交回复
热议问题