Various script type attributes and vanilla js that differentiate user agents

I usually write a script tag without a type attribute. However, I've seen several different types of script. So I tested different types and everything looked the same to me, except that I was deliberately injecting the wrong type.

<script>alert(1)</script>
<script type="text/javascript">alert(2);</script>
<script type="text/ecmascript">alert(3);</script>
<script type="application/ecmascript">alert(4);</script>
<script type="application/javascript">alert(5);</script>
<script type="foo/javascript">alert(6);</script>
<script type="text/html">alert(7);</script>
<script type="application/x-javascript">alert(8);</script>

      

Questions:

  • If the script is given without a type attribute, as I am guessing, is it using the browser's default type?

  • It looks like the text / * is used for browsers and app / * is used for mobile apps. I'm right?

  • In a script type attribute, what makes the difference between javascript and ecmascript?

  • I asked a question about vanilla js and pure js, but the question has been deleted. So I guess they are the same (maybe too obvious). Is this really the case? Then why use different names (Javascript, Vanilla Javascript)? Using vanilla script as an external resource, can we work with browser independent javascripting?

+3


source to share


1 answer


1. If the script is given without a type attribute, as I am guessing, is it using the browser's default type?

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

.

Relevant excerpt from the specification :

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

Thus, the best way to host your script for all browsers is:

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

      

2. It looks like the text / * is used for browsers and app / * is used for mobile apps. I'm right?



If the element is script

included in the HTML document then the attribute value type

should be omitted or "text/javascript"

even in a mobile app. There have been various cases in the past where other types have been proposed, but now the standard "text/javascript"

.

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

There is no difference. As you might know, ECMAScript is the name of the normalized standard that JavaScript is based on. Not only is the "text/javascript"

standard way of referring to javascript in a script element, but your browser only has one JS engine. It cannot choose to try and choose another one depending on the taste, which you can specify in the type attribute.

4. I asked a question about vanilla js and pure js, but the question has been deleted. So I guess they are the same (maybe too obvious). Is this really the case? Then why use different names (Javascript, Vanilla Javascript)? Using vanilla script as an external resource, can we work with browser independent javascripting?

"Vanilla something", in English, refers to the basic scent of something. When computed, this usually refers to a language with no additions or plugins. Here it means JavaScript without additional libraries (like jQuery). This expression is often meant as a way of confirming that many things are possible without libraries, even when people often think that a library is needed. The vanilla-js site humorously supports this argument and which is another reason you might read the expression "vanilla js" a lot. So, of course, "vanilla js" is just JavaScript, that is, "pure js".

Please note that "vanilla js" is not particularly "browser independent" as many js libraries have as their primary goal to provide a uniform layer that hides differences between browsers (most of the time, features are available in all browsers except IE).

+2


source







All Articles