上的文档执行写:这是不可能的,除非它被明确地打开以写入一个文件从异步加载外部脚本。 [英] Execute write on doc: It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

查看:3825
本文介绍了上的文档执行写:这是不可能的,除非它被明确地打开以写入一个文件从异步加载外部脚本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以后的页面加载执行,像这样加载某些脚本:

I am trying to load a certain script after page load executes, something like this:

function downloadJSAtOnload(){
            var element = document.createElement("script");
            element.src = "scriptSrc";
            document.body.appendChild(element);
        }

         if (window.addEventListener)
                  window.addEventListener("load", downloadJSAtOnload, false);
            else if (window.attachEvent)
                  window.attachEvent("onload", downloadJSAtOnload);
            else window.onload = downloadJSAtOnload;

虽然这个剧本似乎执行并下载scriptSrc',右body标签结束前添加,它在控制台(铬)产生以下消息(不是一个错误)

And while this script seems to execute and download 'scriptSrc', and append it right before the end of the body tag, it yields the following message (not an error) in the console (chrome)

未能在文档执行写:这是不可能的,除非它被明确打开写入到一个文件从异步加载外部脚本

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

这是什么意思,甚至?而且我应该做不同的东西吗?即使我得到预期的行为?

What does this even mean? And am I supposed to do something differently? Even though I get the expected behavior?

推荐答案

这是异步加载脚本可能会运行后该文档已被完全解析和关闭。这一点,你不能使用的document.write()从这样一个脚本(以及技术上可以,但你想要什么也不会做)。

An asynchronously loaded script is likely going to run AFTER the document has been fully parsed and closed. This, you can't use document.write() from such a script (well technically you can, but it won't do what you want).

您将需要通过创建DOM元素,然后将它们插入到一个特定的父母与<替换有明确DOM操作该脚本任何的document.write()语句code> .appendChild()或 .insertBefore()或设置 .innerHTML 或某种机制直接DOM操作这样的。

You will need to replace any document.write() statements in that script with explicit DOM manipulations by creating the DOM elements and then inserting them into a particular parent with .appendChild() or .insertBefore() or setting .innerHTML or some mechanism for direct DOM manipulation like that.

例如,而不是在内嵌脚本这种类型code的:

For example, instead of this type of code in an inline script:

<div id="container">
<script>
document.write('<span style="color:red;">Hello</span>');
</script>
</div>

您会在动态加载脚本中使用这个来代替内嵌脚本上面:

You would use this to replace the inline script above in a dynamically loaded script:

var container = document.getElementById("container");
var content = document.createElement("span");
content.style.color = "red";
content.innerHTML = "Hello";
container.appendChild(content);

或者,如果是,你需要的只是追加到容器中没有其他内容,你可以简单地这样做:

Or, if there was no other content in the container that you needed to just append to, you could simply do this:

var container = document.getElementById("container");
container.innerHTML = '<span style="color:red;">Hello</span>';

这篇关于上的文档执行写:这是不可能的,除非它被明确地打开以写入一个文件从异步加载外部脚本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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