How can I wait for a page to load in XUL application?
I have two windows, pressing a button in one of them opens some page
in another window.
anotherWindow.location.href = "http://somewhere/something";
I've searched through google and found several ways to wait until this
page is loaded:
anotherWindow.onload = ...
anotherWindow.addActionListener('load', ...)
anotherWindow.document.addActionListener('load', ...)
anotherWindow.document.getElementById('appcontent').addActionListener('DOMContentLoaded'), ...)
but non of those worked. I think it is because when I do:
anotherWindow.location.href = ...
the page is loaded into anotherWindow which discards all my javascript
action listeners.
How can this be solved? Maybe it is possible to load a page in a
"blocking way", something like:
anotherWindow.loadBlocking('http://somewhere/something')
?
The example xullap is here:
http://brylant.iit.pwr.wroc.pl/~srahuzau/xulapp.zip
After unpacking:
xulrunner application.ini
or if it is Firefox3
firefox -app application.ini
After pressing Load http://www.google.com is loaded into another
window. How can I wait until it is fully loaded or at least check
whether it is fully loaded?
I am using nsIWebProgress interface to wait for loading.
Implementation may look like this:
const listener = {
/* nsIWebProgressListener implementaion */
QueryInterface: function(iid){
/* provide nsIWebProgressListener interface */
},
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
/* check for (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) */
};
anotherWindow.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIWebProgress)
.addProgressListener(listener, Ci.nsIWebProgress.NOTIFY_STATE_WINDOW);
> > How can I wait for a page to load in XUL application?
> I am using nsIWebProgress interface to wait for loading.
> Implementation may look like this:
[skip]
Thanks.. Can all this stuff be done in Javascript or those interfaces
are not available there?
> > > How can I wait for a page to load in XUL application?
> > I am using nsIWebProgress interface to wait for loading.
> > Implementation may look like this:
>
> [skip]
> Thanks.. Can all this stuff be done in Javascript or those interfaces
> are not available there?
Gee.. This IS Javascript, I'm so stupid. ;)
> anotherWindow.getInterface(Ci.nsIWebNavigation)
> .QueryInterface(Ci.nsIWebProgress)
> .addProgressListener(listener, Ci.nsIWebProgress.NOTIFY_STATE_WINDOW);
OK, it worked.. Although it's not that simple. I will post my working
solution here if anybody will search for something similar. The code
you provided doesn't work if anotherWindow is just a XUL <window>. I
had to do something like that:
=== Cut ===
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="anotherWindow" title="AnotherWindow" width="800"
height="600"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/
there.is.only.xul">
<browser id="browser-content" type="content-primary"
src="about:blank" flex="1"/>
</window>
=== Cut ===
Now, having this window with a browser embedded I can do:
const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
const nsIWebProgress = Components.interfaces.nsIWebProgress;
const nsIWebProgressListener =
Components.interfaces.nsIWebProgressListener;
const nsISupportsWeakReference =
Components.interfaces.nsISupportsWeakReference;
var myBrowser = anotherWindow.document.getElementById("browser-
content");
var wp = myBrowser.webNavigation.QueryInterface(nsIWebProgress);
wp.addProgressListener(listener, nsIWebProgress.NOTIFY_STATE_WINDOW);
// Now when I loadURI like:
myBrowser.webNavigation.loadURI("http://www.google.com",
nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
Assuming my listener is:
var listener = {
QueryInterface: function (iid) {
if (iid.equals(nsIWebProgressListener) ||
iid.equals(nsISupportsWeakReference))
return this;
throw Components.results.NS_NOINTERFACE;
},
onStateChange: function (webProgress, request, stateFlags, status) {
if (stateFlags & nsIWebProgressListener.STATE_STOP) {
alert('OK');
}
}
};
I do achieve what I need: alert is shown.