是目前正在运行的脚本的最后一个`script`元素? [英] Is it the last `script` element the currently running script?

查看:94
本文介绍了是目前正在运行的脚本的最后一个`script`元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设当脚本运行时,文档中最后一个脚本元素*是当前运行的脚本吗?

Is it safe to assume that the last script element* in the document when the script runs** is the currently running script?

例如,我想创建一个可以放在页面正文中的脚本,并在同一个地方显示一个元素。我正在做这样的事情:

For example, I want to create a script that can be dropped anywhere in the body of of a page and display an element in the same place. I'm doing something like this:

function getCurrentScriptElement() {
    var scripts = document.getElementsByTagName('script');
    return scripts[scripts.length - 1];
}

var script = getCurrentScriptElement();
var view = document.createElement('span');

/* Put stuff in our view... */

script.parentNode.insertBefore(view, script);

假设脚本在文档的正文中,这是安全吗? getCurrentScriptElement 函数将始终返回正在运行的脚本?如果没有,怎么做?

Assuming the script is in the body of the document, is this "safe?" Will the getCurrentScriptElement function always return the running script? If not, how can it be done?

我不想把脚本绑定到特定的id属性或类似的东西,我想要这样做位置。

I'd like to do this without tying the script to a specific id attribute or similar, I'd like it to just be positional.

我创建了一个例子这里,其中拉入此脚本。一个答案表明,其他脚本可能会创建一个这样的例子会中断的情况。在这个例子中添加其他脚本可能会破坏它吗?

I created an example here that pulls in this script. One answer suggested that other scripts could create a condition where an example like this would break. Is it possible to add other scripts to this example that will break it?

有人建议使用 defer async 属性可能会破坏它。任何人都可以提供一个这样的脚本可能如何工作的例子?

It was suggested that other scripts with defer or async attributes could break this. Can anyone give an example of how such a script might work?

据了解, defer 意味着加载DOM首先运行脚本,并使用 defer 标签。在另一个脚本元素上出现的 defer 属性将如何影响 getCurrentScriptElement 的行为?

As I understand it, defer means load the DOM first, and then run the script with the defer tag. How would the defer attribute appearing on another script element affect the behavior of getCurrentScriptElement?

async ,据我所知,这意味着开始抓取该脚本并继续同时解析DOM,不要等待...但是当它点击我的脚本时,它应该仍然停止等待,对吧?

async, as I understand it, means start fetching that script and keep parsing the DOM at the same time, don't wait... but when it hits my script it should still stop and wait, right?

我没有看到任何人都可以影响它,任何人都可以提供一个例子?

I don't see how either one could affect it, can anyone provide an example?

*为此目的,我只对外部脚本感兴趣。

* I'm only interested in external scripts for the purpose of this question.

**不是整个文档中最后一个脚本元素,而是最后一个脚本元素在文件中运行时。文件的其余部分不应该加载,对吧?

** Not the last script element in the entire document, but the last script element in the document at the time when it runs. The rest of the document shouldn't be loaded yet, right?

推荐答案

这不是一个绝对的保证。查看这个JSFiddle: http://jsfiddle.net/jAsek/

It's not an absolute guarantee no. Check out this JSFiddle: http://jsfiddle.net/jAsek/

<!DOCTYPE html>
<title>Test case</title>
<div>
    <p>At the start</p>
    <script id="first">
        var scr1 = document.createElement("script");
        scr1.setAttribute("id", "early");
        document.body.appendChild(scr1);
    </script>
    <p>After the first script</p>
    <script id="second">
        function getCurrentScriptElement() {
            var scripts = document.getElementsByTagName('script');
            return scripts[scripts.length - 1];
        }

        alert(getCurrentScriptElement().id);
    </script>
    <p>At the end</p>
</div>

这里的警报报告了早期注入脚本的ID,而不是当前正在运行的脚本的id 第二。

Here the alert reports the id of the injected script "early", not the id of currently running script "second".

内部和外部脚本之间没有实际的区别。

There's no practical difference between internal and external scripts.

这篇关于是目前正在运行的脚本的最后一个`script`元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆