External JavaScript Include In HTML

26th December 2007

To include a JavaScript file into a HTML page you can use the script tag with the src parameter pointing towards the source code file. However, there is a subtle difference between the script tag in HTML and XHTML. This is because the language attribute is not supported in XHTML, so if you just copy the code from HTML to XHTML the page won't validate. The solution here is to just leave it out.

For HTML

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

For XHTML

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

The type attribute is the mime type of the script, which is always text/javascript. Although there is come discussion about what the mime type is supposed to be, best results are usually obtained by just sticking with text/javascript.

Add new comment

The content of this field is kept private and will not be shown publicly.