Skip to: content, navigation

by Ian Lloyd

src (HTML attribute)

Browser support full matrix
IE5.5+ FF1+ Saf1.3+ Op9.2+
Full Full Full Full
Spec
Depr. Version
No HTML 4
src="URL of external script file"

Example

Referring to a script file named "complex.js":

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

Description

Using JavaScript becomes much more powerful when common functionality can be shared across a web site rather than embedding code in at page level over and over again. To do this, all you need to do is create your JavaScript in a standalone file, save it with the .js extension and then refer to it within the script element using the src attribute. The same rules regarding locating the resource apply as per other external resources (images, links to other pages), whereby "../" in the src indicates to the request the file from a directory one level up from the current directory (that the referring page resides in), and "/" indicates that the browser should start looking in the domain document root.

If the src attribute contains a valid URI, the browser should then ignore any content inside the opening <script> and closing </script> tags, for example:

<script type="text/javascript" src="/scripts/complex.js">
//the next line should be ignored if /scripts/complex.js exists
alert("Well howdi ho, pardners!");
</script>

When presented with the above, the annoying alert should not be displayed to the user, only the contents of complex.js should be processed (and thankfully, all the browsers tested honored this).

Value

The location of the destination document, relative to the referencing document, relative to the server root or as a complete URI containing the http:// protocol, the server name and the path to the document on that server.

Compatibility

IE5.5Full
6.0Full
7.0Full
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Full
2.0Full
3.0Full
Opera9.2Full
9.5Full

Causes no compatibility issues. It has excellent support across all tested browsers.

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters