简体   繁体   English

document.write在动态加载的javascript中不起作用

[英]document.write not working in dynamically loaded javascript

I've written a bookmarklet which injects an external javascript just before the closing body tag. 我写了一个书签,它在关闭body标签之前注入了一个外部javascript。

The injected script contains two lines: 注入的脚本包含两行:

document.write("");
alert("hello");

The alert works. 警报起作用。 The document.write has no effect. document.write无效。

What gives? 是什么赋予了? This should clear the page, but the page remains unchanged. 这应该清除页面,但页面保持不变。

If you need to overwrite the entire document with an empty string. 如果需要用空字符串覆盖整个文档。 you need to call document.write in window.onload handler: 您需要在window.onload处理程序中调用document.write

window.onload = function(){
   document.write("");
}

When the page is rendering document.write just outputs the string where it is called, but after the page load it replaces the document with the string. 当页面呈现document.write只会在调用它的位置输出字符串,但是在页面加载后,它将用字符串替换文档。

or call document.body.innerHTML = ''; 或致电document.body.innerHTML = '';

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

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