Execute code on every page load (like window.onload)

8 views
Skip to first unread message

Jannes Meyer

unread,
Dec 4, 2009, 3:47:04 PM12/4/09
to mozilla-labs-prism
Hi guys!
I think I need your help here. My problem is that I want to execute
some code in my webapp.js everytime a new page is loaded to modify
some part of the DOM tree. But the load() function is only executed
when starting prism, so it would just work for the first page. As soon
as another page gets loaded my modifications are gone and I want to
reapply them.

How can I set up an event handler for every page load in prism? Using
window.onload doesn't work, because the window object is destroyed and
recreated, when a new page is loading.

Unfortunately I have no knowledge in internal Firefox-APIs, so I'm
stuck here with my javascript.
I didn't find any help in the documentation or with google.

Thanks in advance,
Jannes

Daniel Hohenberger

unread,
Dec 4, 2009, 4:18:13 PM12/4/09
to mozilla-l...@googlegroups.com
Hi.

I have a problem with my setup of Prism and local tomcat. Everything works fine
untill I unplug my network cable. Then I get an error message that the Server
does not respond when I try to load a page (or enter data in a field validated
via AJAX). When I plug it back in, everything works fine again.

Any ideas?

Jannes Meyer

unread,
Dec 4, 2009, 6:39:06 PM12/4/09
to mozilla-labs-prism
Btw, I just found out that window.document.getElementById() is not
working at all, so how can I even modify the dom?

Jason Miller

unread,
Dec 5, 2009, 11:34:03 PM12/5/09
to mozilla-l...@googlegroups.com
var webapp = (function(){
  var window;
  return {
    init : function(w) {
      window = w;
      // now you have a stored reference to the window object that seemed to become unavailable previously
      window.document.getElementById();
    }
  };
})();

function load() {
  webapp.init(window);
}

Jason Miller
519.872.0797 // developIT // Jason Miller Design
Developer of amoebaOS, Shutterborg, Delitweet & more


--

You received this message because you are subscribed to the Google Groups "mozilla-labs-prism" group.
To post to this group, send email to mozilla-l...@googlegroups.com.
To unsubscribe from this group, send email to mozilla-labs-pr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mozilla-labs-prism?hl=en.



Jason Miller

unread,
Dec 5, 2009, 11:36:14 PM12/5/09
to mozilla-l...@googlegroups.com
Are you using a local URL, or a domain name?
It sounds like Prism is going to the network when you are making the AJAX call.
Jason Miller
519.872.0797 // developIT // Jason Miller Design
Developer of amoebaOS, Shutterborg, Delitweet & more


Daniel Hohenberger

unread,
Dec 6, 2009, 4:41:12 AM12/6/09
to mozilla-l...@googlegroups.com
Jason Miller schrieb:
> Are you using a local URL, or a domain name?
> It sounds like Prism is going to the network when you are making the
> AJAX call.

I'm using a local URL like http://127.0.0.1:8083/my.page

Jannes Meyer

unread,
Dec 6, 2009, 8:30:07 AM12/6/09
to mozilla-l...@googlegroups.com
I'm sorry, I think you misunderstood me. I'll try to explain my problem with an example.

Let's look at this code:
function load() {
    window.alert("Loaded");
}

This gives me only one alert when the webapp is loaded, but i want an alert everytime a new page is loaded.
How do I achieve this? I already tried this without success:
function load() {
    window.onload = function() {
        window.alert("Loaded");
    }
}

Because this doesn't work I concluded that the window object obviously gets recreated.

Greetings,
Jannes

Matthew Gertner

unread,
Dec 15, 2009, 11:31:22 AM12/15/09
to mozilla-l...@googlegroups.com
The problem is probably that Prism goes into offline mode when you
unplug the cable. Unfortunately Mozilla apps can't access even local
servers when in offline mode (see
https://bugzilla.mozilla.org/show_bug.cgi?id=339814). You could fix
this by writing an XPCOM component that forces the app out of offline
mode (setting the offline attribute of the nsIIOService to false)
whenever it goes into offline mode. I did this for one client who had
a similar problem and it works fine.

Matt

Matthew Gertner

unread,
Dec 15, 2009, 11:33:55 AM12/15/09
to mozilla-l...@googlegroups.com
The problem is that the window object is the content window and not the browser window. So yes, it gets recreated whenever you load a new page. I think we probably want a new callback (e.g. pageLoad()) that is called whenever a new page is loaded. I actually thought someone had already filed a bug about this in Bugzilla, but I took a quick look and I couldn't find it.

Matt

jeremy

unread,
Dec 28, 2009, 11:21:27 AM12/28/09
to mozilla-labs-prism
I currently have this working. Credit for the implementation should go
to Jannes, but I just thought it should be posted here for posterity.
Here's the solution we're using:

var Ci = Components.interfaces;
var Cc = Components.classes;

function load() {
var wm = Cc["@mozilla.org/appshell/window-mediator;
1"].getService(Ci.nsIWindowMediator);
var mainWindow = wm.getMostRecentWindow("navigator:browser");
mainWindow.addEventListener("DOMContentLoaded", function()
{ <Your Function> }, false);
}

Hope this helps someone.

Jeremy

On Dec 15, 11:33 am, Matthew Gertner <matthew.gert...@gmail.com>
wrote:


> The problem is that the window object is the content window and not the
> browser window. So yes, it gets recreated whenever you load a new page. I
> think we probably want a new callback (e.g. pageLoad()) that is called
> whenever a new page is loaded. I actually thought someone had already filed
> a bug about this in Bugzilla, but I took a quick look and I couldn't find
> it.
>
> Matt
>

> On Sun, Dec 6, 2009 at 2:30 PM, Jannes Meyer <jannes.me...@gmail.com> wrote:
> > I'm sorry, I think you misunderstood me. I'll try to explain my problem
> > with an example.
>
> > Let's look at this code:
> > function load() {
> >     window.alert("Loaded");
> > }
>

> > This gives me only one alert when* the webapp is loaded*, but i want an
> > alert everytime a *new page is loaded.
> > *How do I achieve this? I already tried this without success:


> > function load() {
> >     window.onload = function() {
> >         window.alert("Loaded");
> >     }
> > }
>
> > Because this doesn't work I concluded that the window object obviously gets
> > recreated.
>
> > Greetings,
> > Jannes
>
> > On Sun, Dec 6, 2009 at 5:34 AM, Jason Miller <ja...@developit.ca> wrote:
>
> >> var webapp = (function(){
> >>   var window;
> >>   return {
> >>     init : function(w) {
> >>       window = w;
> >>       // now you have a stored reference to the window object that seemed
> >> to become unavailable previously
> >>       window.document.getElementById();
> >>     }
> >>   };
> >> })();
>
> >> function load() {
> >>   webapp.init(window);
> >> }
>
> >>  Jason Miller

> >> 519.872.0797 // developIT <http://developit.ca/> // Jason Miller Design<http://jasonmillerdesign.com/>
> >> *Developer of amoebaOS <https://amoebaos.com/>, Shutterborg<http://shutterb.org/>,
> >> Delitweet <http://delitweet.com/> & more*

> >>> mozilla-labs-pr...@googlegroups.com<mozilla-labs-prism%2Bunsu...@googlegroups.com>


> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/mozilla-labs-prism?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "mozilla-labs-prism" group.
> >> To post to this group, send email to mozilla-l...@googlegroups.com.
> >> To unsubscribe from this group, send email to

> >> mozilla-labs-pr...@googlegroups.com<mozilla-labs-prism%2Bunsu...@googlegroups.com>


> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/mozilla-labs-prism?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "mozilla-labs-prism" group.
> > To post to this group, send email to mozilla-l...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > mozilla-labs-pr...@googlegroups.com<mozilla-labs-prism%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages