このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

HTMLScriptElement: src プロパティ

Baseline 広く利用可能 *

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

* この機能の一部は、対応レベルが異なる場合があります。

srcHTMLScriptElement インターフェイスのプロパティで、外部スクリプトの URL を表す文字列です。文書内に直接スクリプトを埋め込む代わりに使用することができます。

これは <script> 要素の src 属性を反映したものです。

文字列です。

このコードは URL が https://example.com であるウェブサイト上で動作していると想定します。

html
<script id="script-with-src" type="module" src="proxy.php?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fmain.js"></script>
<script id="script-without-src" type="module"></script>
js
const script_with_src = document.getElementById("script-with-src");
console.log(script_with_src.src); // 出力: "https://example.com/main.js"
const script_without_src = document.getElementById("script-without-src");
console.log(script_without_src.src); // 出力: ""

仕様書

仕様書
HTML
# dom-script-src

ブラウザーの互換性