Script src property

HTML DOM Script src property Script object

Get the URL of the external script:

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

x The output is:

//www.w3cschool.cn/jsref/demo_script_src.js

Try it out . . .

Definitions and usages

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

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

If you want to run the same JavaScript on multiple pages in your Web site, you should create an external JavaScript file instead of writing the same script over and over again. Save the script file .js the src extension, and then refer to it in the src property in the label.

Note: External script files cannot contain labels.

Note: Refers correctly to the external script file that you written to the script.


Browser support

HTML DOM Script src property HTML DOM Script src property HTML DOM Script src property HTML DOM Script src property HTML DOM Script src property

Src properties are supported by all major browsers.


Grammar

Return the src property:

scriptObject .src

Set the src property:

scriptObject .src= URL

The property value

value describe
URL The URL of the external script file.

Possible value:

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

Technical details

Return value: String, which represents the URL of the external script file. Returns the full URL, including protocols, such as http://.


Related articles

HTML Reference Manual: HTML slt;script> src property


HTML DOM Script src property Script object