Latest web development tutorials

HTML DOM Script src attribute

Script Object Reference Script Object

Examples

Get external URL script:

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

x The output is:

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

try it"

Definition and Usage

Sets or returns the value of the src attribute of the script src attribute.

src attribute specifies the URL of an external script file.

If you want to run the same JavaScript on the site of several pages, you should create an external JavaScript file, instead of repeating the same scripting. The extension .js script file to be saved, and then refer to it using the src attribute in <script> tag.

Note: The external script file can not contain <script> tag.

Note: The script is written correctly reference your external script file.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the src attribute.


grammar

Back src attribute:

scriptObject .src

Setting the src attribute:

scriptObject .src= URL

Property Value

描述
URL 外部脚本文件的 URL。

可能的值:

  • 绝对 URL - 指向另一个网站(比如 src="http://www.example.com/example.js")
  • 相对 URL - 指向网站内的一个文件(比如 src="/scripts/example.js")

technical details

return value: String that represents the URL of an external script file. Returns the complete URL, including the protocol (such as http: //).


related articles

HTML Reference Manual: HTML <Script> the src attribute


Script Object Reference Script Object