External Script

Description

You can separate scripts into separate files and load them using the script element.

Suppose we have a file called simple.js, its contents are shown in the following code.


document.write("This is from  the   external script");

The file contains a single statement. Then we can use the src attribute in the script element to reference this file.

A script element must be empty if it uses the src attribute. You can't use the same script element to define an inline script and an external script.

Example


<!DOCTYPE HTML>
<html>
<head>
   <script src="simple.js"></script>
</head>
<body>
    <p>This is a test.</p>
    <a href="http://java2s.com">Visit  java2s.com</a>
</body>
</html>

The value for the src attribute is the URL of the script file that you want to load. The simple.js file is in the same directory as the HTML file.

An end tag for the script element is needed, even though the element has no content. If you use a self-closing tag when referencing an external script, the browsers will ignore the element and not load the file.





















Home »
  HTML CSS »
    HTML »




HTML Introduction
HTML Document
HTML Section
HTML Group Content
HTML Text Marker
HTML Table
HTML Form
HTML Embed