External JS Files

JavaScript FAQ | Basic Syntax & General Questions  

Question: Can I include JavaScript code from external JS files, rather than embedding all my scripts within HTML pages?

Answer: Yes. To do so, create a new file with the extension

, for example,
. Put your JavaScript code in this file; do not include opening and closing
tags in the

To embed

into your Web page, use these tags in your HTML code:
<script type="text/javascript" src="myscript.js">
In practice, you may find it convenient to create
files containing JavaScript functions that you reuse on multpile webpages. Usually (but not always) the
tags specifying the included JavaScript files are placed in the
section of the page.

Historical note:
Some very old browsers (e.g. Navigator 2.x and Explorer 3.x) could not load external JavaScript files – they simply ignored the


See also:

Copyright © 1996-2018, JavascriptFAQ.net.