Document: thuộc tính 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.
Thuộc tính Document.currentScript trả về phần tử <script> có script đang được xử lý và không phải là một JavaScript module. (Đối với modules, sử dụng import.meta thay thế.)
Cần lưu ý rằng thuộc tính này sẽ không tham chiếu đến phần tử <script> nếu mã trong script đang được gọi như một callback hoặc trình xử lý sự kiện; nó chỉ tham chiếu đến phần tử trong khi đang được xử lý ban đầu.
Giá trị
Một HTMLScriptElement hoặc null.
Ví dụ
Ví dụ này kiểm tra xem script có đang được thực thi bất đồng bộ hay không:
js
if (document.currentScript.async) {
console.log("Executing asynchronously");
} else {
console.log("Executing synchronously");
}
Đặc tả kỹ thuật
| Specification |
|---|
| HTML> # dom-document-currentscript-dev> |
Tương thích trình duyệt
Xem thêm
import.meta<script>- Sự kiện
afterscriptexecutecủaDocument - Sự kiện
beforescriptexecutecủaDocument