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 возвращает элемент <script>, код которого в данный момент выполняется и не является модулем JavaScript. Для модулей необходимо использовать import.meta.

Важно отметить, что ссылка на элемент <script> будет присутствовать только во время первоначального исполнения скрипта. Иными словами, если код в скрипте вызывается как функция обратного вызова или обработчик события, то Document.currentScript будет равен null.

Значение

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

Смотрите также