I am working on a minimal Firefox extension that loads a webpage in a XUL iframe. (I also tried html:iframe , but met identical results.) It may take a while for the page to load completely - and I'm trying to get the DOMContentLoaded event, which should appear before the load event.
(The main reason is that I am trying to introduce a CSS stylesheet, and this should be done immediately after the DOMContentLoaded event, instead of waiting for the page to be "unexplored" before the load event. It can also be used for other reasons, so the alternatives are specific for CSS are not a viable practice.)
However, so far I have been able to receive load events, not DOMContentLoaded or readyState .
The problem should be easily reproduced using the XUL below and just enter the XUL path in the Firefox location bar, given its URL chrome:// (similar to this ):
<?xml version="1.0"?> <!DOCTYPE window> <window xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script type="application/x-javascript"> window.addEventListener("DOMContentLoaded", function(event){ try{ var outElem = document.getElementById("out"); var out = function(s){ outElem.value += s + "\n"; }; var frameTest = document.getElementById("iframe-test"); out("start: " + frameTest.contentDocument.readyState); frameTest.addEventListener("DOMContentLoaded", function(e){ out("DOMContentLoaded! " + e); }, true); frameTest.addEventListener("readystatechange", function(e){ out("readystatechange: " + e); }, true); frameTest.addEventListener("load", function(e){ out("load: " + e + ", state: " + frameTest.contentDocument.readyState); }, true); out("all listeners registered, frame location: " + frameTest.contentWindow.location + ", state: " + frameTest.contentDocument.readyState); }catch(e){ alert(e); } }, true); </script> <iframe id="iframe-test" type="content" src="http://www.google.com" height="400" width="400"/> <textbox id="out" rows="10" cols="80" multiline="true"/> </window>
The output that I get in the debug text box is:
start: uninitialized all listeners registered, frame location: about:blank, state: uninitialized load: [object Event], state: complete
I cannot understand why I am not getting any DOMContentLoaded! outputs DOMContentLoaded! or readystatechange:
Another minimal example that also doesn't work is available at https://gist.github.com/2985342 .
The pages I linked to include:
I mentioned this on irc.mozilla.org/#extdev and was able to get the answer "Work for everyone else" and "Best use of listener capture." - thatโs why I have a third useCapture argument set to true in all of the above addEventListener calls (although I havenโt noticed any difference with setting this value to false or without its full).
I am looking for the โright wayโ for this without resorting to polling on contentDocument.readyState .
Update: This and other similar examples work as expected when fetching through the "Real-Time XUL Editor" (part of https://addons.mozilla.org/en-US/firefox/addon/extension-developer/ ), but not when Download as chrome://test/content/test.xul file chrome://test/content/test.xul . Is it true that when loading through the location bar, there are limited privileges, etc. that cause this problem?