YouTip LogoYouTip

Prop Script Src

HTML DOM Script src Attribute

HTML DOM Script src Attribute

-- Learning is not just about technology, but also about dreams!

JavaScript Reference

Overview

JavaScript Objects

Browser Objects

DOM Objects

HTML Objects

HTML DOM Script defer Attribute

HTML DOM Script text Attribute

Script src Attribute

Script Object Reference Script Object

Example

Get the URL of an external script:

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

x outputs:

http://www.w3cschool.cc/jsref/demo_script_src.js

Try it yourself Β»


Definition and Usage

The src attribute sets or returns the value of the src attribute of a script.

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

If you want to run the same JavaScript on several pages in your site, 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 refer to it using the src attribute in the <script> tag.

Note: The external script file cannot contain script tags.

Note: Reference the external script file correctly that you write into the script.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the src attribute.


Syntax

Return the src property:

scriptObject.src 

Set the src property:

scriptObject.src=URL

Property Values

Value Description
URL The URL of the external script file. Possible values:
  • An absolute URL - points to another website (like src="http://www.example.com/example.js")
  • A relative URL - points to a file within a website (like src="/scripts/example.js")

Technical Details

Return Value: A String, representing the URL of the external script file. Returns the full URL, including the protocol (like http://).

Related Articles

HTML Reference: HTML <script> src Attribute


Script Object Reference Script Object

← Prop Script TextProp Script Defer β†’