English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

Complete List of HTML Tags

HTML: <script> src Attribute

The src attribute specifies the URL of an external script file. If the same JavaScript needs to be run on multiple pages of a website, it is better to create an external JavaScript file rather than writing the same script over and over again. Save the script file with the .js extension and then refer to it using the src attribute of the <script> tag.

 HTML <script> tag

Online Example

src points to an external JavaScript file:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>HTML: Usage of the src Attribute in <script>-Basic Tutorial(oldtoolbag.com)</title> 
</head>
<body>
<script src="demo_script.js"></script>
</body>
</html>
Test and see ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the src attribute.

Definition and Usage

The src attribute specifies the URL of the external script file.

If you want to run the same JavaScript on multiple pages of your website, you should create an external JavaScript file instead of writing the same script over and over. Save the script file with a .js extension, and then reference it using the src attribute in the <script> tag.

Note: External script files cannot contain <script> tags.

Note: The exact location of the external script file is where you write your script.

HTML 4.01 with HTML5Differences

None.

Syntax

<script src="URL">

Attribute value

ValueDescription
URLURL to link an external script file.

Possible values:

  • Absolute URL - points to another website (for example src="http://www.example.com/example.js")

  • Relative URL - points to a file within the website (for example src="/scripts/example.js")

 HTML <script> tag