简体   繁体   English

使用RequireJS模块的“ document.write”问题

[英]Problems with “document.write” using RequireJS modules

I have a module that needs to execute a document.write action in order to print a banner once the page is loaded. 我有一个模块,该模块需要执行document.write动作,以便在页面加载后打印横幅。

If I do this using the old-school way, I get no problems. 如果我使用老式的方法,则不会有任何问题。 The banner is printed inside the div. 横幅打印在div内。

<div id="banner">
    <script> addAdvert() </script>
</div>

<script>
function addAdvert(){
  srcScript = 'http://www.urltoscript.js';
  document.write('<script type=\"text/javascript\" src=\"'+srcScript+'" ><\/script>');
}
</script>

But If I try this using a require js module (kind of this): 但是,如果我使用require js模块尝试这种操作(类似此类):

addAvert: function() {
             var srcScript = options.urltoScript
             document.write('<script type=\"text/javascript\" src=\"'+srcScript+'" ><\/script>');
}

It executes the document.write and render all the document printing only the banner on the entire document... 它执行document.write并渲染所有文档,仅在整个文档上打印横幅。

I have try this alternative: 我尝试了这种替代方法:

addAvert: function() {
    var srcScript = options.urltoScript

          var bsa = document.createElement('script');
             bsa.type = 'text/javascript';
             bsa.async = true;
             bsa.src = srcScript;
             $("#banner").append(bsa);
 }

Judging by your comment, the script which you are trying to load from the imaginary URL http://www.urltoscript.js also uses document.write . 根据您的评论判断,您尝试从虚拟URL http://www.urltoscript.js加载的脚本使用document.write Either you should change this script to not use document.write or you should abandon the idea of loading it asynchronously, because document.write does not work reliably when invoked asynchronously . 您应该更改此脚本以不使用document.write或者应该放弃异步加载的想法,因为document.write在异步调用时不能可靠地工作

You've already discovered when you tried require([bsa.src]) (as you mentioned in a comment) that you cannot call document.write from a script that is loaded asynchronously. 您在尝试require([bsa.src]) (如您在注释中提到require([bsa.src])时已经发现,无法从异步加载的脚本调用document.write (Also note that just doing require([bsa.src]) is not going to work unless the source code at the other end is an AMD-module or you defined a shim for it.) (还请注意,除非另一端的源代码是AMD模块或您为其定义了shim ,否则仅执行require([bsa.src])不会起作用。)

Your first attempt at loading through RequireJS did not produce an error message about document.write being loaded asynchronously because the script element that loads it is not itself asynchronous. 您第一次通过RequireJS进行加载的尝试并未产生关于document.write异步加载的错误消息,因为加载它的script元素本身并不是异步的。 However, it completely blanked your page. 但是,它完全使您的页面空白。 This is because if you call document.write after the page has finished loading, the browser may implicitly call document.open and this may blank your page. 这是因为,如果在页面加载完成后调用document.write ,浏览器可能会隐式调用document.open ,这可能会使页面空白。

The upshot here is that you cannot reliably use document.write with asynchronous code. 结果是您不能可靠地将document.write与异步代码一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM