<script type="text/javascript"> - javascript

Is there any difference between
<script type="text/javascript">
and
<script>
tags ?

The type attribute is no longer a required attribute in most browsers.
From MDC:
type
This attribute identifies the
scripting language of code embedded
within a script element or referenced
via the element’s src attribute. This
is specified as a MIME type; examples
of supported MIME types include
text/javascript, text/ecmascript,
application/javascript, and
application/ecmascript. If this
attribute is absent, the script is
treated as JavaScript.

There is no difference between those two tags. Prior to HTML5 you need to explicitly add 'text/javascript'. On HTML5 you can skip that part.

Most browsers will default type to text/javascript, but other values are allowed, see
http://www.w3schools.com/tags/tag_script.asp
Also, as for W3C specifications
As HTML does not rely on a specific scripting language, document authors must explicitly tell user agents the language of each script. This may be done either through a default declaration or a local declaration.

As per W3C standard its essential. As HTML does not rely on a specific scripting language, document authors must explicitly tell user agents the language of each script. This may be done either through a default declaration or a local declaration.

According to CSS-Tricks:
<script type="text/javascript">
//some javascript here
</script>
The type attribute is the standard and correct way to identify and
tell the browser what kind of script the tag contains. Sometimes
you'll see code that uses both the language and type attribute. As far
as I know that's never necessary.
Really specific explanation from the spec, language is an "obsolete
but conforming" feature.
but
<script>
//some javascript here
</script>
No attributes at all. This is the HTML5 way of handling script tags
that contain JavaScript. It's just assumed that the type is
text/javascript. If it's not (I've never even seen a different type of
script) you'll need to change it with the type attribute. I recommend
this is you are using HTML5.

Related

What is the significance of the type property in a JavaScript script tag?

In JavaScript, both <script src="some-javascript-file.js"></script> and <script src="some-javacript-file.js" type="text/javascript"></script> work in my web pages. This applies to both external JavaScript code and scripts defined within html. I am aware of the text and application issues. What then is the significance of the type property itself in the JavaScript <script> tag if things work when omitted?
As of HTML 5, the type attribute is optional.
If it is omitted, the browser defaults to JavaScript..
For inline scripts, if you aren't putting JavaScript in the script element, then you need to specify the type to stop it being treated as JavaScript.
For linked scripts, if you aren't linking to a JavaScript script, then you need to specify the type to stop the browser making an HTTP request for it, seeing the content-type isn't JS, and then throwing an error.
For browser compatibility reasons.
The type attribute replaced the language attribute back in the days of JavaScript 1+ and you could previously specified VBScript for the scripting language of choice but very rarely do you ever see that today.
The type attribute itself is optional, it was used to indicate the content type also called MIME type. The text/javascript value is the default if you omit the type attribute in your script tag.

Why does script-type have "text/" prepended to it?

Why does script-type have "text/" prepended to it?
For example:
<script type="text/javascript" />
<link type="text/css"/>
<script id="entry-template" type="text/x-handlebars-template">
Probably the standard javascript/vbscript/css are defined as
<MIME-media-type>/<MIME-sub-type>
What about Handlebars - why does it follow the same format? (and same with few other libs)
And if I wish to add a custom type for my open-source-lib (e.g: nirman)... should it be written as:
<script type="nirman" />
/// OR
<script type="text/nirman" />
?
Thanks
Why does script-type have "text/" prepended to it?
It doesn't … text/javascript was obsoleted in favour of application/javascript.
What about Handlebars - why does it follow the same format?
… but your question seems to be more about why MIME types come in two parts. That is defined by the MIME specification to divide the MIME type into a general category (from a small selection on infrequently updated choices) and a specific type.
And if I wish to add a custom type for my open-source-lib (e.g: nirman)... should it be written as
Neither.
Firstly, if you are writing a scripting language, then you should use application, not text:
text -- textual information … subtypes are to be used for enriched text in forms where application software may enhance the appearance of the text, but such software must not be required in order to get the general idea of the content
application -- some other kind of data, … information to be processed by an application.
Secondly, since you are making this up and don't have a public standard for it, the subtype should be prefixed with X
A media type value beginning with the characters "X-" is a private
value, to be used by consenting systems by mutual agreement. Any
format without a rigorous and public definition must be named with an
"X-" prefix,
So you should use: application/x-nirman
Media type (MIME type) names generally consist of a major media type and a subtype, separated by a slash “/”. This is specified in RFC 2406.
The major type chosen for JavaScript is text, because JavaScript code is text. It can be directly read by human beings – who need to understand JavaScript of course, that’s why it is not plain text (text/plain). Later, various other media types have been proposed for JavaScript, for rather theoretical reasons, but text/javascript is what all browsers recognize.
Handlebars seems to use text/x-handlebars-template.
If you want to use, within a script element, code that is not JavaScript code but will be processed by JavaScript code that interprets it as data of some kind (possibly as program code in some language), then the safest bet is to use text/plain. Not because of logic, but because of a statement in HTML5 CR about scripting languages. It lists a few media type names that “must not be interpreted as scripting language”, and among the alternatives, text/plain is probably the least illogical.
The point here is to prevent browsers from executing the script element content as JavaScript (or as VBScript), so you need to specify a type attribute, with a value that will make the browser refrain from doing what browsers normally do with script elements. The browser will then just store the content in the DOM, for your JavaScript code to deal with.
Yes, this is illogical if your code is, in some sense, program code, and you are doing your best to tell browsers that it isn’t. But it’s not program code natively interpreted by the browser.
Using text/x-nirman or application/x-nirman would most probably work too. Or anything that does not look suspiciously like referring to JavaScript or VBScript.

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

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 me except I put wrong type intentionally.
<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 script given without type attribute, as I assume, does it uses browser default type?
It looks like text/* is used for browsers and application/* is used for mobile applications. Am I right about it?
In script type attribute, what makes difference between javascript and ecmascript?
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?
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).

Difference between <script> tag with type and <script> without type? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Should I include type=“text/javascript” in my SCRIPT tags?
I was writing HTML and found that even if the type in the script tag is not set to javascript, the javascript code in the tag can still be evaluated.
so I was just wondering what the difference is between the script tag with type and one without?
In HTML 4, the type attribute is required. In my experience, all browsers will default to text/javascript if it is absent, but that behaviour is not defined anywhere. While you can in theory leave it out and assume it will be interpreted as JavaScript, it's invalid HTML, so why not add it.
In HTML 5, the type attribute is optional and defaults to text/javascript:
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".
Most browsers default to text/javascript, but it's always good to be explicit when setting the type.
type
This attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.
MDN docs
It depends upon the browser. It's largely historical when browsers used to support VBScript and Javascript (along with others).
Leaving "type" off can cause errors in some browsers.

What is the difference in various ways of defining a script?

What is the difference in various ways of defining a script?
The ways I am talking about is this
<script>....</script>
<script language="javascript">.....</script>
<script type="text/javascript">...........</script>
Since, they all do same things, what is different?
According to w3c spec, type attribute is required, and determines script language, whereas language attribute (which does more or less the same) is deprecated in favor of type, so you should use type attribute.
<script language="javascript">
HTML 3.2 — The first attempt
<script type="text/javascript">
HTML 4.x and XHTML 1.x — using MIME types for everything. This is the current standard. Use this.
<script>
HTML 5 (draft) — "Ah, too many authors don't care, browsers error recover from it anyway, lets all but give up on the possibility other other embedded scripting languages being used"
In HTML 4.01 the type attribute is required. http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-type-SCRIPT
In practice though, all commonly used browsers default this to text/javascript, so there isn't really a need to specify it unless you care about validators or older versions of IE. The HTML5 spec makes it optional. http://www.whatwg.org/specs/web-apps/current-work/#attr-script-type

Categories