Onload problems - called only once. - Mozilla

This is a discussion on Onload problems - called only once. - Mozilla ; Hello, I wrote an extension. I want the extension to check the DOM everytime the page is fully loaded. Currently I use document.addEventListener("DOMContentLoaded", MyExt.OnLoad, false). The problem with that is that I want the callback to be called once when ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Onload problems - called only once.

  1. Onload problems - called only once.

    Hello,
    I wrote an extension. I want the extension to check the DOM everytime
    the page is fully loaded.
    Currently I use document.addEventListener("DOMContentLoaded",
    MyExt.OnLoad, false). The problem with that is that I want the
    callback to be called once when the page is fully loaded.

    I tried using the window.onload method (using the following code:
    function addLoadEvent(func)
    {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload = function() {
    oldonload();
    func();
    }
    }
    }

    addLoadEvent(MyExt.OnLoad);

    )
    but for some reason the handler is only called once when I start the
    browser (even before the first page is shown).

    What am I doing wrong?

    Thank you

  2. Re: Onload problems - called only once.

    etimor@gmail.com wrote:

    >I wrote an extension. I want the extension to check the DOM everytime the page is fully loaded.
    >Currently I use document.addEventListener("DOMContentLoaded", MyExt.OnLoad, false). The problem with that is that I want the callback to be called once when the page is fully loaded.
    >
    >I tried using the window.onload method (using the following code:
    > function addLoadEvent(func)
    > {
    > var oldonload = window.onload;
    > if (typeof window.onload != 'function') {
    > window.onload = func;
    > } else {
    > window.onload = function() {
    > oldonload();
    > func();
    > }
    > }
    > }
    >
    > addLoadEvent(MyExt.OnLoad);
    >
    >)
    >but for some reason the handler is only called once when I start the browser (even before the first page is shown).
    >
    >What am I doing wrong?
    >
    >

    You're listening for the wrong sort of loads on the wrong DOM node. Most
    people do something like getBrowser().addEventListener("load",
    MyExtOnLoadListener, true);

    --
    Warning: May contain traces of nuts.

  3. Re: Onload problems - called only once.

    On Jun 3, 1:17 pm, Neil wrote:
    > eti...@gmail.com wrote:
    > >I wrote an extension. I want the extension to check the DOM everytime the page is fully loaded.
    > >Currently I use document.addEventListener("DOMContentLoaded", MyExt.OnLoad, false). Theproblemwith that is that I want the callback to be calledoncewhen the page is fully loaded.

    >
    > >I tried using the window.onloadmethod (using the following code:
    > > function addLoadEvent(func)
    > > {
    > > var oldonload = window.onload;
    > > if (typeof window.onload!= 'function') {
    > > window.onload= func;
    > > } else {
    > > window.onload= function() {
    > > oldonload();
    > > func();
    > > }
    > > }
    > > }

    >
    > > addLoadEvent(MyExt.OnLoad);

    >
    > >)
    > >but for some reason the handler is only calledoncewhen I start the browser (even before the first page is shown).

    >
    > >What am I doing wrong?

    >
    > You're listening for the wrong sort of loads on the wrong DOM node. Most
    > people do something like getBrowser().addEventListener("load",
    > MyExtOnLoadListener, true);
    >
    > --
    > Warning: May contain traces of nuts.


    Thank you very much. Now it works.
    One question though - for some reason the event handler is called more
    than once on some sites (like www.nytimes.com for example). Is there
    an event that is only called once when the page is fully loaded?

  4. Re: Onload problems - called only once.

    On Jun 3, 1:17 pm, Neil wrote:
    > eti...@gmail.com wrote:
    > >I wrote an extension. I want the extension to check the DOM everytime the page is fully loaded.
    > >Currently I use document.addEventListener("DOMContentLoaded", MyExt.OnLoad, false). Theproblemwith that is that I want the callback to be calledoncewhen the page is fully loaded.

    >
    > >I tried using the window.onloadmethod (using the following code:
    > > function addLoadEvent(func)
    > > {
    > > var oldonload = window.onload;
    > > if (typeof window.onload!= 'function') {
    > > window.onload= func;
    > > } else {
    > > window.onload= function() {
    > > oldonload();
    > > func();
    > > }
    > > }
    > > }

    >
    > > addLoadEvent(MyExt.OnLoad);

    >
    > >)
    > >but for some reason the handler is only calledoncewhen I start the browser (even before the first page is shown).

    >
    > >What am I doing wrong?

    >
    > You're listening for the wrong sort of loads on the wrong DOM node. Most
    > people do something like getBrowser().addEventListener("load",
    > MyExtOnLoadListener, true);
    >
    > --
    > Warning: May contain traces of nuts.


    Thank you very much. Now it works.
    One question though - for some reason the event handler is called more
    than once on some sites (like www.nytimes.com for example). Is there
    an event that is only called once when the page is fully loaded?

  5. Re: Onload problems - called only once.

    etimor@gmail.com wrote:

    >One question though - for some reason the event handler is called more than once on some sites (like www.nytimes.com for example). Is there an event that is only called once when the page is fully loaded?
    >
    >

    What's happening here is that the event fires for [i]frames as well as
    when the page is fully loaded. (This also applies to DOMContentLoaded
    and web progress notifications.) If you only want the current tab you
    need to check that the event's target is content.document. If you want
    top-level loads on all tabs then event.target.defaultView.top ==
    event.target.defaultView is probably the easiest check.

    --
    Warning: May contain traces of nuts.

  6. Re: Onload problems - called only once.

    On Jun 3, 3:55 pm, Neil wrote:
    > eti...@gmail.com wrote:
    > >One question though - for some reason the event handler is called more than once on some sites (likewww.nytimes.comfor example). Is there an event that is only called once when the page is fully loaded?

    >
    > What's happening here is that the event fires for [i]frames as well as
    > when the page is fully loaded. (This also applies to DOMContentLoaded
    > and web progress notifications.) If you only want the current tab you
    > need to check that the event's target is content.document. If you want
    > top-level loads on all tabs then event.target.defaultView.top ==
    > event.target.defaultView is probably the easiest check.
    >
    > --
    > Warning: May contain traces of nuts.


    Sorry for the dumb question, but when I try in the load hander to
    write event.target.defaultView.top is says that
    event.target.defaultView has no properties (and
    event.target.defaultView is undefined).

  7. Re: Onload problems - called only once.

    etimor@gmail.com wrote:

    >when I try in the load hander to write event.target.defaultView.top it says that event.target.defaultView is undefined.
    >
    >

    Well, maybe I misremembered, and you need to use event.originalTarget,
    or maybe event.target is something else that I've forgotten about; what
    does event.target.toString() return?

    --
    Warning: May contain traces of nuts.

  8. Re: Onload problems - called only once.

    On Jun 4, 12:41 pm, Neil wrote:
    > eti...@gmail.com wrote:
    > >when I try in the load hander to write event.target.defaultView.top it says that event.target.defaultView is undefined.

    >
    > Well, maybe I misremembered, and you need to use event.originalTarget,
    > or maybe event.target is something else that I've forgotten about; what
    > does event.target.toString() return?
    >
    > --
    > Warning: May contain traces of nuts.


    Great, event.originalTarget did the trick.
    Thanks a lot for your help.
    btw, I'm pretty new to the extensions world - is there a good link or
    book you can refer me to?
    Again, thank you very much for all you help.

+ Reply to Thread