简体   繁体   中英

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.originalTarget;

    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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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