JavaScript is executed client-side, on the browser. Like CSS, 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:
- <head>
- <title>An HTML Page With Linked Javascript<title>
- <script type="text/javascript" src="assets/scripts/prettify.js"></script>
- </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.)
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
- </script>
The second and less preferable option is to write the JavaScript embedded, again in the <head> section:
- <script type="text/javascript">
- /* JavaScript code goes here */
- </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:
- <script type="text/javascript">
- <!--
- /* JavaScript code goes here */
- -->
- </script>
The HTML comment around embedded JavaScript code accomplishes two things:
- It protects very old browsers that may not understand JavaScript, and will attempt to run the code as HTML.
- 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.
so we don't need the jQuery minimum length anymore:) cool!


