From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/document/currentscript/index.html | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 files/zh-cn/web/api/document/currentscript/index.html (limited to 'files/zh-cn/web/api/document/currentscript') diff --git a/files/zh-cn/web/api/document/currentscript/index.html b/files/zh-cn/web/api/document/currentscript/index.html new file mode 100644 index 0000000000..a54cb27eb8 --- /dev/null +++ b/files/zh-cn/web/api/document/currentscript/index.html @@ -0,0 +1,68 @@ +--- +title: document.currentScript +slug: Web/API/Document/currentScript +tags: + - API + - DOM + - Property + - Reference + - 参考 + - 属性 +translation_of: Web/API/Document/currentScript +--- +
{{ApiRef("DOM")}}
+ +

Document.currentScript 属性返回当前正在运行的脚本所属的 {{HTMLElement("script")}} 元素。调用此属性的脚本不能是 JavaScript 模块,模块应当使用 {{JSxRef("Statements/import%2Emeta", "import.meta")}} 对象。

+ +

值得注意的是,如果当前正在执行的代码是被其他代码作为回调函数或者事件处理函数调用的,那么 currentScript 属性不会指向任何 {{HTMLElement("script")}} 元素,而是会返回 null。这个属性只在脚本被解析后首次运行时有效。

+ +

语法

+ +
var curScriptElement = document.currentScript;
+
+ +

示例

+ +

下例演示了如何检测当前正在执行脚本的 {{HTMLElement("script")}} 元素是否是以异步模式执行的。

+ +
if (document.currentScript.async) {
+  console.log("Executing asynchronously");
+} else {
+  console.log("Executing synchronously");
+}
+ +

View Live Examples

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("HTML WHATWG", "dom.html#dom-document-currentscript", "Document.currentScript")}}{{Spec2("HTML WHATWG")}}Initial definition
+ +

浏览器兼容性

+ + + +
{{Compat("api.Document.currentScript")}}
+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf