Script srcProperties

Script 对象参考手册Script Object

Example

Get the URL of an external script:

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

xThe result is:

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

Try it Yourself »

Definition and Usage

The src property sets or returns the value of the script's src attribute.

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 repeatedly. Save the script file with a .js extension, and then reference it using the src attribute in the <script> tag.

Note:The external script file cannot contain <script> tags.

Note:Correctly reference the external script file containing your 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

Attribute Values

Value Description
URL The URL of the external script file.

Possible values:

  • Absolute URL - points to another website (e.g., src="http://www.example.com/example.js")
  • Relative URL - points to a file within a website (e.g., src="../scripts/example.js.html")

Technical Details

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


Related Articles

HTML Reference Manual:HTML <script> src attribute


Script 对象参考手册Script Object

Other Extensions