demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

How To Write JavaScript On An HTML Page

is executed client-side, on the browser. Like , it can be written either inline, embedded, or linked, and (just like CSS) those methods are in order of preference in terms of power and adaptability. A link to a JavaScript file is written inside the <head> section of the page on which it is used:

  1. <head>
  2. <title>An HTML Page With Linked Javascript<title>
  3. <script type="text/javascript" src="assets/scripts/prettify.js"></script>
  4. </head>

Independent JavaScript files must have a .js extension in the filename. (You will occasionally see a language attribute used when linking to a JavaScript file, but that is now deprecated, and is not valid in HTML.)

It is also possible to link to a JavaScript file outside of your own domain: this is frequently done with JavaScript frameworks such as jQuery or Prototype, both to centralize code and to potentially speed up load times. (Cross-domain files like the jQuery library sourced from Google will be cached by the browser: if the user has previously visited a site that uses the same technique for the same file, the cached version of the file will be used instead, rather than downloading it again.)

  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
  2. </script>

The second and less preferable option is to write the JavaScript embedded, again in the <head> section:

  1. <script type="text/javascript">
  2. /* JavaScript code goes here */
  3. </script>

Note that you cannot write HTML or CSS directly inside JavaScript; like CSS, using another language between the opening and closing <script> tags, or in the linked JavaScript file, will cause it to stop working. The only HTML you can use in embedded script is a very special case:

  1. <script type="text/javascript">
  2. <!--
  3. /* JavaScript code goes here */
  4. -->
  5. </script>

The HTML comment around embedded JavaScript code accomplishes two things:

  1. It protects very old browsers that may not understand JavaScript, and will attempt to run the code as HTML.
  2. More practically, it allows us to continue to validate the HTML page without confusing the validator.

You do not, as a general rule, write JavaScript code inline, for reasons we shall cover shortly.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.