Script src attribute

Definition and usage

Src Attribute to set or return the script's Src attribute The value.

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

If you want to run the same JavaScript on multiple pages of a website, you should create an external JavaScript file instead of writing the same script over and over again. 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.

See also:

HTML Reference Manual:HTML <script> src Attribute

HTML Reference Manual:HTML <script> Tag

Example

Get the URL of the external script file:

var x = document.getElementById("myScript").src

Try it yourself

Syntax

Return the src attribute:

scriptObject.src

Set the src attribute:

scriptObject.src = URL

Attribute value

Value Description
URL

URL of an external script file.

Possible values:

  • Absolute URL - Points to another website (such as src="http://www.example.com/example.js")
  • Relative URL - Points to a file within the website (such as src="/scripts/example.js"))

Technical Details

Return Value: A string value representing the URL of an external script file. Returns the entire URL, including the protocol (such as http://).

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support