src (HTML attribute)
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
| IE | 5.5 | Full |
|---|---|---|
| 6.0 | Full | |
| 7.0 | Full | |
| Firefox | 1.0 | Full |
| 1.5 | Full | |
| 2.0 | Full | |
| Safari | 1.3 | Full |
| 2.0 | Full | |
| 3.0 | Full | |
| Opera | 9.2 | Full |
| 9.5 | Full |
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.

