W3cubDocs

/Web APIs

HTMLScriptElement: src property

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

* Some parts of this feature may have varying levels of support.

The src property of the HTMLScriptElement interface is a string representing the URL of an external script; this can be used as an alternative to embedding a script directly within a document.

It reflects the src attribute of the <script> element.

Value

A string.

Examples

Assume the code is running on a website whose URL is https://example.com.

<script id="script-with-src" type="module" src="/main.js"></script>
<script id="script-without-src" type="module"></script>
const script_with_src = document.getElementById("script-with-src");
console.log(script_with_src.src); // Output: "https://example.com/main.js"
const script_without_src = document.getElementById("script-without-src");
console.log(script_without_src.src); // Output: ""

Specifications

Specification
HTML>
# dom-script-src>

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
src 1 12 1 ≤12.1 3 18 4 ≤12.1 1 1.0 4.4 1
enforces_trusted_types 83 83 135 69 26 83 No 59 26 13.0 83 26

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement/src