Various script type attributes and vanilla js, what makes difference in user agents

前端 未结 1 1694
無奈伤痛
無奈伤痛 2021-01-20 20:10

I usually write script tag without type attribute. However I saw around several different types of script. So I have tested with different types and all looked the same to m

相关标签:
1条回答
  • 2021-01-20 20:51

    1. If script given without type attribute, as I assume, does it uses browser default type?

    Yes. All browsers always assumed it to be the case and it has now been normalized in HTML5. According to the w3.org the type attribute defaults to "text/javascript".

    Relevant extract from the spec :

    The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".

    So the best way to put your script, for all browsers, is this :

    <script>alert(1);</script>
    

    2. It looks like text/* is used for browsers and application/* is used for mobile applications. Am I right about it?

    If the script element is included in an HTML document then the value of the type attribute should be omitted or "text/javascript", even in a mobile application. There were various cases in the past where other types were suggested but now the standard is "text/javascript".

    3.In script type attribute, what makes difference between javascript and ecmascript?

    There is no difference. As you might know ECMAScript is the name of the normalized standard upon which is based JavaScript. Not only is "text/javascript" the standard way of referring to javascript in the script element but your browser has only one JS engine. It can't choose to try and select another one depending of a flavor you might specify in the type attribute.

    4. I asked question about between vanilla js and pure js, but the question has been removed. So my guess is they are the same(too obvious maybe). Are the really same?, then why use different names(Javascript, Vanilla Javascript)? By using vanilla script as an external resource, can we do browser independent javascripting?

    "Vanilla something", in English, refers to the basic flavor of something. In computing it commonly refers to a language without additions or plugins. Here it means JavaScript without additional libraries (for example jQuery). This expression is often meant as a way to reassert many things are possible without libraries even while people often think the library is needed. The vanilla-js site humorously supports this argument and that's one more reason you may have often read the "vanilla js" expression. So, of course, "vanilla js" is just JavaScript, that is "pure js".

    Note that "vanilla js" isn't especially "browser independent" as many js libraries have as primary goal to provide an uniform layer hiding differences between browsers (most often features available in all browsers except IE).

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