20

For adding JavaScript to HTML, I have seen people use

<script language=javascript>

and

<script type="text/javascript">

It doesn’t seem like whether the script is embedded or external influences this decision.

Which one is preferred and why?

3

2 Answers 2

31

<script language="javascript"> was used in very old browsers, and is deprecated.

<script type="text/javascript"> is the HTML 4 standard.

In HTML 5, the type parameter is optional (text/javascript is the default), so you can just do <script>.

As a neat hack, if you put an invalid type, the script won't be ran, but you can still read the data in JavaScript. Some template libraries do this.

Sign up to request clarification or add additional context in comments.

4 Comments

@Foo: You can still use deprecated features, and mix standards. It's not good practice, and isn't valid HTML, but most browsers will run it anyway.
@RocketHazmat Without some solid source on the matter I would not trust every browser out there to refrain from trying to run a script block just because it has a weird type attribute set. And even if it does work all across the board I still think "horrendous" would be a better adjective than "neat" ;-)
@eBusiness: Fair enough. I've just seen it done, and assumed it was fine. I can check to see what the spec says ^_^
Today, the recommended way to embed schema.org metadata in a web page is by embedding a JSON fragment in a script tag with type="application/ld+json". So I guess it is save to say we can now trust browsers not to run it, as it is embedded this way on millions of sites.
8

The language attribute is deprecated. Use type only. You don't need to specify type in HTML5, it's javascript per default.

4 Comments

In HTML 5, you don't even need type! Just <script> :-)
@AgustinMeriles that's valid in HMTL. Just not in XHTML.
@Christoph - unless this person is using XML, i think they're safe.
If they are using a browser, they are safe. Just stick your XHTML doctype on your horribly invalid HTML and it will still work more or less the same. Browsers might switch to some non-standards mode, but I think even that has been more or less phased out already. Browsers always only really used one parser/rendering engine anyway, this just supported some 'standards mode /quirks mode' toggle and that was it. XHTML just never really made it past the W3C validator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.