document.write将不会在网页上显示文本 [英] document.write won't show the text on the web page

查看:141
本文介绍了document.write将不会在网页上显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这个简单的greasemonkey脚本

When I run this simple greasemonkey script

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

仅前两个语句起作用,第三和第四条语句不起作用.

Only the first two statements work, the third and fourth statement do nothing.

这可能是什么问题?

推荐答案

document.write()似乎只能从Greasemonkey间歇地工作-可能是由于沙箱造成的.

It seems that document.write() only works intermittently from Greasemonkey -- maybe due to the sandbox.

我还没有找到万无一失的方法来重复该问题.

I haven't found a foolproof recipe for duplicating the issue yet.

但是,基于DOM的方法似乎总是可行.使用此代码:

However, the DOM-based approach always seems to work. Use this code:

//--- Erase everything from the page's body:
var b           = document.body;
var p           = b.parentNode;
p.removeChild (b);


//--- Add our new text or HTML.
var newB        = document.createElement ("BODY");
newB.innerHTML  = "Hello";
p.appendChild (newB);




PS:我也更新了其他答案.




PS: I updated my other answer as well.

这篇关于document.write将不会在网页上显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆