requirejs加载文件中不能放document. write语句么?
发布于 10 年前 作者 kingapple 4701 次浏览 最后一次编辑是 8 年前

怎么一放,页面马上变空白了呢? 放在所有require()语句前,那个时候页面输出流应该没有完成吧,那个时候调用document.write怎么也会导致页面空白? 迷惑了。。。

8 回复

异步加载的js中 放置 document.write就不行了

异步调用有先后,完成顺序不一定和调用顺序一样。 但是不调用异步接口,中间直接写的还是阻塞代码么? 如果是的话,那应该先于异步代码执行啊,也不至于导致页面空白的。。。 可是页面就无耻的空白了:(

这个跟后端没关系, 现在的浏览器编写,不要用document.write(),用innerHTML或者createTextNode。 如果页面load后,调用document.write(),会把所有html内容替换为document.write()内的字符串。

你试试用console.log()

个人的浅见:require.js异步调用时,文件执行在回调中完成,按照: “异步加载的外引js,js里面写document.write, 但是document.write放在回调中, 这时候document.write触发是通过回调,没有具体位置,这时候如果页面已经close了, 会主动调用一次document.open,然后才write,这时候就是重写了。” 具体可以参考require.js源码:execb function。 仅供参考。

open不是重新打开一个页面么?为了写入信息打开一个新的页面,好像没这么干的

是 document.open ,打开当前文档;在 dom 构建完毕之后 document.write 就是会重写整个 document 的

@kingapple 那是document.write默默完成的,而且是打开当前页,具体可参考:Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call. From:https://developer.mozilla.org/en-US/docs/Web/API/document.write

回到顶部