aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/currentscript
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/document/currentscript
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/document/currentscript')
-rw-r--r--files/zh-cn/web/api/document/currentscript/index.html68
1 files changed, 68 insertions, 0 deletions
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
+---
+<div>{{ApiRef("DOM")}}</div>
+
+<p><code><strong>Document.currentScript</strong></code> 属性返回当前正在运行的脚本所属的 {{HTMLElement("script")}} 元素。调用此属性的脚本<a href="https://github.com/whatwg/html/issues/997">不能是 JavaScript 模块</a>,模块应当使用 {{JSxRef("Statements/import%2Emeta", "import.meta")}} 对象。</p>
+
+<p>值得注意的是,如果当前正在执行的代码是被其他代码作为回调函数或者事件处理函数调用的,那么 <code>currentScript</code> 属性不会指向任何 {{HTMLElement("script")}} 元素,而是会返回 <code>null</code>。这个属性只在脚本被解析后首次运行时有效。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <var>curScriptElement</var> = <var>document</var>.currentScript;
+</pre>
+
+<h2 id="示例">示例</h2>
+
+<p>下例演示了如何检测当前正在执行脚本的 {{HTMLElement("script")}} 元素是否是以异步模式执行的。</p>
+
+<pre class="brush:js">if (document.currentScript.async) {
+ console.log("Executing asynchronously");
+} else {
+ console.log("Executing synchronously");
+}</pre>
+
+<p><a href="/samples/html/currentScript.html">View Live Examples</a></p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("HTML WHATWG", "dom.html#dom-document-currentscript", "Document.currentScript")}}</td>
+ <td>{{Spec2("HTML WHATWG")}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<div>{{Compat("api.Document.currentScript")}}</div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{JSxRef("Statements/import%2Emeta", "import.meta")}}</li>
+ <li>{{HTMLElement("script")}}</li>
+ <li>{{DOMxRef("document.onafterscriptexecute")}}</li>
+ <li>{{DOMxRef("document.onbeforescriptexecute")}}</li>
+</ul>