bigbobr
bigbobr

Reputation: 355

window.onload on firefox 30

I need some help with firefox addon. I've got a function that gets some values from the page and uses them to fill new window text area.

var ticket = document.getElementsByName('ticket_id');
var addr = document.getElementsByName('address_id');
var text = "Your address is "+addr[0].value+", ticket number is "+ticket+",";
smsWin = window.open("http://mysite.net/", "New window", "height=250");

smsWin.onload = function () {
  smsWin.document.getElementById('textsms').innerHTML = text;
};

The function is called by an action button in Firefox

var button1 = require("sdk/ui/button/action").ActionButton({
  id: "style-tab1",
  label: "Do magic",
  icon: "./icon-64.png",
  onClick: function() {
    require("sdk/tabs").activeTab.attach({
      contentScriptFile: self.data.url("close.js")
    });
  }
});

It works perfect on ScratchPad and in the addon on Firefox 29. But in latest Firefox 30 the smsWin.onload function part doesn't seems to be working (even if i put alert('1') inside), while the window still opens. Any ideas on how to fix it?

UPD. Also tried this

smsWin.addEventListener("load", function() {
  smsWin.document.getElementById('textsms').value = text;
}, false);

And it doesnt work in a plugin as well (but works on ScratchPad and FF29). Changing load to DOMContentLoaded cnahges nothing

Upvotes: 2

Views: 524

Answers (2)

bigbobr
bigbobr

Reputation: 355

OK, I've done some workaround. Seems like window.onload part doesnt work in FF30 cause of secutity updates. But if the window is an iframe it works perfectly. So I just modified needed pages with var smsWin = document.createElement('iframe'); and so on, and suddenly smsWin.onload = function () {...} works perfectly fine.

Upvotes: 1

Noitidart
Noitidart

Reputation: 37238

The reason it works from scratchpad is because you are running the code window.open("http://mysite.net/", "New window", "height=250"); in the mode of "Environment" > "Content"

If you set "Environment" to "Browser" in scratchpad then you will see it won't work.

You can't do window.open from addon code. If you want to open a window you have to use Services.ww: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWindowWatcher

See how to open window with features here: http://forums.mozillazine.org/viewtopic.php?f=19&t=428535&p=2321231&hilit=openWindow#p2321231

  var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
                 .getService(Components.interfaces.nsIWindowWatcher)

  ww.openWindow(null, "chrome://browser/content/browser.xul", "_blank",
       "chrome,all,dialog=no", "http://www.gemme.pl/");


Ok i see you are doing add-sdk and that the window opening is not the problem but the window loading. Try using DOMContentLoaded.

Although sdk confuses the heck out of me. Because when a window opens the load that triggers on window is when xul finishes loading. Then you have the page that loads. But try the DOMContentLoaded in place of the load and let me know how it works.

Upvotes: 1

Related Questions