You are here: Reference > JavaScript > client-side > HTML DOM > properties > src (script)

src property (script)

Browser support:
Sets or retrieves the location of a file containing scripting code.

Syntax:

object.src;
You can find the related objects in the Supported by objects section below.
This property is read/write.
HTML page for this property: src

Possible values:

String that sets or retrieves the URL of the script file. It can be an absolute or relative path as well. Relative paths are relative to the base URL. The base URL is the location of the current document, but it can be overridden by the base tag.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the src attribute:
Code
javascript.js
<head>
    <script type="text/javascript" src="javascript.js"></script>
</head>
<body>
    <button onclick="HelloWorld ();">Call HelloWorld function</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the src property:
Code
javascript.js
<head>
    <script type="text/javascript" id="baseJS" src="javascript.js"></script>
    <script type="text/javascript">
        function GetSrc () {
            var script = document.getElementById ("baseJS");
            alert (script.src);
        }
    </script>
</head>
<body>
    <button onclick="GetSrc ();">Get script location!</button>
    <button onclick="HelloWorld ();">Call HelloWorld function</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content