javascript 如何在 document.write 中包含脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17542595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:42:39  来源:igfitidea点击:

How to include a script with document.write?

javascriptdocument.write

提问by JMW

We need to add a script to our web application. It basically adds an corporate menu.

我们需要向我们的 Web 应用程序添加一个脚本。它基本上添加了公司菜单。

so we've received a script to include in the body of our webapp:

所以我们收到了一个脚本来包含在我们的 web 应用程序的主体中:

<!-- BEGIN NAVIGATION -->
<script type="text/javascript" src="https://intranet.local/?getCorporateJsMenu"></script>
<!-- END NAVIGATION -->

And the content of https://intranet.local/?getCorporateJsMenubasically looks like this:

https://intranet.local/?getCorporateJsMenu的内容基本上是这样的:

document.write('<script type="text/javascript" src="..."></script>');
//....
document.write('<div>');
document.write('<ul>');
//...
document.write('<li><a href="...">...</a></li>');
//...
document.write('</ul>');
document.write('</div>');

after having placed the <!--NAVIGATION--><script...directly into the html body we were experiencing severe page load performance problems.

在将<!--NAVIGATION--><script...直接放入 html 正文后,我们遇到了严重的页面加载性能问题。

so our idea was to add the menu with JS, when everything has already been loaded with something like this:

所以我们的想法是用 JS 添加菜单,当一切都已经加载了这样的东西时:

var script=document.createElement('script');
script.type='text/javascript';
script.src='https://intranet.local/?getCorporateJsMenu';
var domparent=jQuery('#header').get();
domparent[0].appendChild(script);

with firebug we see, that the script element has been added to the html and the script has been loaded from the network, but somehow the document.write of the loaded script doesn't get executed?

使用 firebug 我们看到,脚本元素已添加到 html 并且脚本已从网络加载,但不知何故加载脚本的 document.write 没有被执行?



UPDATE: we cannot modify the contents of https://intranet.local/?getCorporateJsMenusince it comes from 3rd party

更新:我们无法修改https://intranet.local/?getCorporateJsMenu的内容,因为它来自 3rd 方

回答by Teemu

This happens because the script execution stops at the first found literal </script>tag, no matter if it was enclosed in the parenthesis. You need to obfuscate the ending scripttag, for example:

发生这种情况是因为脚本执行在第一个找到的文字</script>标记处停止,无论它是否包含在括号中。您需要混淆结束script标记,例如:

document.write('<script type="text/javascript" src="..."><\/script>');

However, it seems you use also jQuery, why not use the jQuery methods to load a script and add the content to a page rather than document.write()?

但是,您似乎也使用 jQuery,为什么不使用 jQuery 方法来加载脚本并将内容添加到页面而不是document.write()

回答by dandavis

here is an example of hiHymaning document.write() to bring 21st century loading performance to legacy scripts:

这是劫持 document.write() 以将 21 世纪的加载性能带到遗留脚本的示例:

<script>
var writes=[];
document.write=[].push.bind(writes);
</script>
<div id=targ></div>
<script type="text/javascript" src="https://intranet.local/?getCorporateJsMenu"></script>
<script>
 document.getElementById("targ").innerHTML=writes.join(" ");
</script>

i've used the patterns to support ads on a SPA site, as well as to cache embeds, and in one case to modify the content's style before injecting.

我已经使用这些模式来支持 SPA 网站上的广告,以及缓存嵌入,并且在一种情况下在注入之前修改内容的样式。

回答by Robert Hoffmann

Try this:

试试这个:

document.write('<scr'+'ipt type="text/javascript" src="..."></scr'+'ipt>');