Script srcProperties
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
![]()
![]()
![]()
![]()
![]()
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:
|
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
Other Extensions
Script Object