开发者

Injected script doesn't show alert in Firefox 4

The code below is from my extension. It works in FF 3.6, but not in FF 4.0. In FF 4.0 it works only for page "about:home". What's wrong?

// set up our page load handler
window.addEventListener(
  "load",
  function () {
    gBrowser.addEventListener("load", examplePageLoad, true);
  },
  false
);

function examplePageLoad(event)
{
    var doc = event.originalTarge开发者_如何学JAVAt;

    if (!(doc instanceof HTMLDocument))
        return;  // ignore images, etc

    if (doc.defaultView.frameElement)
        return;  // ignore frames and iframes

    var jsspan = doc.createElement("span");

    jsspan.innerHTML="<script type=\"text/javascript\">\
\
function test_function() { alert('hello'); }\
test_function();\
\
</script>";
doc.body.appendChild(jsspan);

} 


Try creating a script element instead of using innerHTML in a span. And then create a text node containing the code to be injected, append it to the script, then append the script to body.

What are you trying to do anyway? Why not reference an external JS?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜