如何异步加载具有document.write的第三方javascript标记 [英] How to load third-party javascript tag asynchronously which has document.write
问题描述
我们发布了一段javascript代码,例如< script src =http://ours.com/some.js>< / script>
哪些网站所有者在 http://example.com 上放置了他们的网站,在这个javascript标记中我们想要动态地包含第三方js其中可以包含document.write,但当然如果我们尝试通过常规方法包含它,
var script_tag =使用document.createElement( '脚本');
script_tag.type ='text / javascript';
script_tag.src =http://third-party.com/some.js;
document.getElementById('target')。appendChild(script_tag);
我们从浏览器收到警告,
警告:从异步加载的
外部脚本调用document.write()被忽略。
我们如何解决这个问题?请记住,我们并不真正控制第三方脚本,因此我们无法更改其中的逻辑。我们正在寻找一种适用于所有浏览器的解决方案。
在已加载的文档上加载脚本的问题(而不是让浏览器忽略 document.write()
)是你要删除所有现有的HTML。请参阅此示例,以便了解具体情况或更多信息。有关document.write()方法的详细信息,请参阅文档页面。
虽然我知道这可能不是你期望得到的答案,但我相信你运气不好,因为重写脚本不是一种选择。 / p>
出现此与类似的回复是类似的问题。
We give out a piece of javascript tags such as <script src="http://ours.com/some.js"></script>
which site owners put on their site like http://example.com and in this javascript tag we want to dynamically include a third-party js such as which can have document.write in it, but of course if we try to include it by conventional method,
var script_tag = document.createElement('script');
script_tag.type = 'text/javascript';
script_tag.src="http://third-party.com/some.js";
document.getElementById('target').appendChild(script_tag);
we get a warning from browser,
Warning: A call to document.write() from an asynchronously-loaded external script was ignored.
How do we get around this? Keep in mind, we don't really have control over third-party scripts so we can't change the logic in it. We are looking for some solution which can work across all browsers.
The problem with loading a script on a already loaded document (instead of having the browser ignore the document.write()
) is that you would delete all existent HTML. See this example so you can understand exactly what's happening, or for more details look at a documentation page for the document.write() method.
While I know this might not be what you're expecting to get as an answer, I believe you are out of luck since rewriting the script is not an option.
This appears to be a similar question with similar replies.
这篇关于如何异步加载具有document.write的第三方javascript标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!