Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will removing the type from a <script> tag break in any browsers?

In HTML5, you no longer need to include the type in a script tag when you are using JavaScript.

Are any common browsers (IE6+, Firefox 2+, Safari 3+, Opera 9+ or similar) going to break if the type is removed from all the <script> tags?

So, is changing the following:

<script type="text/javascript" src="/path/js.js"></script>
<script type="text/javascript">
...
</script>

to:

<script src="/path/js.js"></script>
<script>
...
</script>

... going to break anywhere?

like image 797
Darryl Hein Avatar asked Aug 14 '10 23:08

Darryl Hein


People also ask

Do script tags need type?

The type attribute in JavaScript is optional since the introduction of HTML5 brought some new improvements. JavaScript became the default language for HTML5 and modern browsers. So, now adding text/javascript isn't required in <script> tag.

Does it matter where the script tag is?

Complete HTML/CSS Course 2022 You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.

What do script tags do?

Definition and Usage. The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Does script tag go outside body?

<script> tag must not be included in a <header> Google Tag Manager Tag should be placed immediately after the opening <body> tag and not inside any other HTML element.


3 Answers

No it won't break any of the popular browsers, including the ones you mention. The <script> tag will work fine without the type attribute, because all popular browsers will default to JavaScript.

Quoting Douglas Crockford:

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

like image 62
Daniel Vassallo Avatar answered Oct 17 '22 22:10

Daniel Vassallo


going to break anywhere?

No

As per HTML4 specification, it won't be a valid markup only as per W3C Validator if you remove type attribute from the <script> tag although your script should still work fine across browsers.

If you are asking in HTML5 perspective, there is no problem in leaving that out.

like image 6
Sarfraz Avatar answered Oct 17 '22 21:10

Sarfraz


The type attribute was useful a long time ago, when JavaScript was not standardized, along with differences between different versions of the same browser. You could use other languages like vbscript, but in the real world nobody has used it in years.

like image 2
Charlie Avatar answered Oct 17 '22 21:10

Charlie