Javascript - External Javascript File


To include JavaScript from an external file, the src attribute is required.

The value of src is a URL linked to a file containing JavaScript code, like this:

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

In this example, an external file named example.js is loaded into the page.

Processing of the page is halted while the external file is interpreted.

In XHTML documents, you can omit the closing tag, as in this example:

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

By convention, external JavaScript files have a .js extension.

A <script> element using the src attribute should not include additional JavaScript code between the <script> and </script> tags.

If both are provided, the script file is downloaded and executed while the inline code is ignored.

The <script> element can include JavaScript files from outside domains.

<script type="text/javascript" src=""></script> 

You can serve up JavaScript from various domains if necessary.

The <script> elements are interpreted in the order in which they appear in the page so long as the defer and async attributes are not present.

The first <script> element's code must be completely interpreted before the second <script> element begins interpretation.