Document: currentScript プロパティ

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.

Document.currentScript プロパティは、現在処理中で、 JavaScript モジュールではないスクリプトの <script> 要素を返します。(モジュールの場合は代わりに import.meta を使用してください。)

重要なことですが、スクリプト内のコードがコールバックまたはイベントハンドラーとして呼び出されている場合は、 <script> 要素を参照しないことに注意してください。初期化時に処理されている要素のみを参照します。

HTMLScriptElement または null です。

次の例では、スクリプトが非同期で実行されているかどうかをチェックしています。

js
if (document.currentScript.async) {
  console.log("非同期で実行中");
} else {
  console.log("同期で実行中");
}

実際の表示を確認

仕様書

Specification
HTML
# dom-document-currentscript-dev

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
currentScript

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報