document.write不会在网页上显示文本

时间:2011-12-16 20:15:08

标签: javascript greasemonkey

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

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

只有前两个语句有效,第三个和第四个语句不起作用。

这里可能有什么问题?

1 个答案:

答案 0 :(得分:1)

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

我还没有找到一个简单的方法来复制这个问题。

但是,基于DOM的方法似乎总能奏效。使用此代码:

//--- 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:我也更新了我的另一个答案。