The correct way to code for a js source?

I think I’m a bit out of the loop on the proper way to form codie for js scripts, and thoughts seem to differ when speaking to others about it. I had this code 10 years ago, but is it still okay please?

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

I normally do this :

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

So I think your code is good.

In html5 you just need this to be valid:

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

The type=“text/javascript” attribute/value was required for validation prior to html5 but has always been unnecessary as browsers know exactly what to do with a script. These days you should omit it and save on code.:slight_smile:

4 Likes

The language attribute is also best left out, as it has been formally deprecated since the release of html5 several years ago too.

1 Like

Wow, it’s cut out quite a bit then. Has the css code been depreciated as well? I usually do :

 <link rel="stylesheet" href="/styles.css">

According to MDN, you could also drop the rel="stylesheet" part, as the browser should assume that’s the case if left out.

I still think it is a good thing to keep the type in there just in case.
But that’s me :slight_smile:

AFAIK the more correct type is
application/javascript

  • but - IE < 8 doesn’t know what to do with it.

as long as the server is set to use application/javascript for scripts.

Note that text/javascript was deprecated a long time ago - the only reason people continued using it was that before IE9 Internet Explorer didn’t run JavaScript but ran jScript instead which would run with text/javascript as the MIME type but not the correct JavaScript one.

I still include the type=“application/javascript” when using script tags as I suspect that the shared hosting I am using has the wrong type set.

1 Like

These are Crockford’s recommendations if it helps.

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.

1 Like

I can’t say I’ve ever done it myself either. It was more a case of pointing out what MDN says browser should assume if that attribute is left out - the implication being that it can be done.

1 Like

The official recommendations can be found at http://tools.ietf.org/html/rfc4329 and specify application/javascript and application/ecmascript as the only two valid alternatives - all the others are labelled as obsolete and problematic.

1 Like

Thank you very much for all the comments. It’s appreciated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.