Javascript Tutorials

How to link to an external javascript file

The code below shows how to link to an external javascript file.

To link to an external javascript file, add the src attribute to the openning <script> tag. The attribute value must be set to the url of an external javascript file with a ".js" extension. The external javascript file should not contain openning and closing <script> tags, only the javascript statements.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Web Development Tutorials.com</title>

<style type="text/css">


</style>

</head>
<body>
<script type="text/javascript" src="externaljavascriptfile.js">

</script>
</body>
</html>

The code above creates a web page which links to an external javascript file with the javascript statement below.

document.write("I now know how to link to an external javascript file!");

Notice that the external javascript file does not contain the openning and closing <script> tags.

The code above creates the text below.

Use the text editor below to practice what you have learned.