3 Replies - 2144 Views - Last Post: 22 November 2010 - 08:20 AM Rate Topic: -----

#1 aubrey_lab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 10

Firefox Extension: load function triggers twice

Posted 18 November 2010 - 12:55 AM

I am new to Firefox extension development.

I have started to work on an extension that triggers when the page has
been loaded. The problem I have is that the event is triggered twice.
Its almost as if two threads are created when the users navigates or
browses web sites.

Why does this happen and how could this be changed to only trigger
once.

I have attached the files.

Here is the browser.xul file

<?xml version="1.0"?>

<!DOCTYPE DOMDropper SYSTEM "chrome://DOMDropper/locale/
translations.dtd">
<overlay id="sample" xmlns="http://www.mozilla.org/keymaster/
gatekeeper/there.is.only.xul">
              <script src="blah.js" />
</overlay>


Here is blah.js

var Blah = function () {

      return {
              init : function () {
                      gBrowser.addEventListener("load", function () {
                              Blah.run();
                      }, false);
              },

              run : function () {
                      alert("Trigger!");
              }
      };
}();
window.addEventListener("load", Blah.init, false);


Is This A Good Question/Topic? 0
  • +

Replies To: Firefox Extension: load function triggers twice

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: Firefox Extension: load function triggers twice

Posted 18 November 2010 - 01:21 AM

If am not mistaken, the load event is fired once for the page load and once for the favicons.

To only execute code on the page load you should be able to just verify that the orignalTarget of the even is the document node.
run : function (e) {
    if(e.originalTarget.nodeName == '#document') {
        alert("Trigger!");
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 aubrey_lab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 10

Re: Firefox Extension: load function triggers twice

Posted 22 November 2010 - 05:10 AM

Hi. Thanks for the reply. I assume the "e" refers to an Event object. I get an e undefined error when If I substitute my code with your code segment. I think that I am missing something somewhere. But your post does point me into a direction.

View PostAtli, on 18 November 2010 - 12:21 AM, said:

If am not mistaken, the load event is fired once for the page load and once for the favicons.

To only execute code on the page load you should be able to just verify that the orignalTarget of the even is the document node.
run : function (e) {
    if(e.originalTarget.nodeName == '#document') {
        alert("Trigger!");
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: Firefox Extension: load function triggers twice

Posted 22 November 2010 - 08:20 AM

Ahh yes, sorry I missed that. I misread it like the run function was a direct callback to an event.

You'd have to pass the Event object down into the function, or better yet, use the Event object in the callback and only call run() on the correct trigger. I'd probably do something more like:
{
	init : function () {
		gBrowser.addEventListener("load", function (e) {
			if(e.originalTarget.nodeName == '#document') {
				Blah.run();
			}
		}, false);
	},

	run : function () {
		alert("Trigger!");
	}
}

Makes more sense to me that way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1