On Page Load

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 02:11, 26 March 2005
Fatalis (Talk | contribs)

<-- Previous diff
Revision as of 00:11, 4 October 2005
Stefie10 (Talk | contribs)
(On tab load moved to On Page Load)
Next diff -->

Revision as of 00:11, 4 October 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

This article is for XUL/JavaScript developers who want to have their code executed each time a new page is loaded in browser/mail.

Creating an overlay

First, you need to create an overlay to one (or more, depending on which applications you target) of the following XUL documents:

Application URI to overlay
Firefox chrome://browser/content/browser.xul
Thunderbird and Mozilla Mail chrome://messenger/content/messenger.xul
Navigator from the Mozilla Suite chrome://navigator/content/navigator.xul

Attaching a script

Attach a script to your overlay, and add a load event listener to appcontent element (browsers) or messagepane (mail) by putting this code in the script:

window.addEventListener("load", function() { myExtension.init(); }, false);

var myExtension = {
  init: function() {
    var appcontent = document.getElementById("appcontent");   // browser
      appcontent.addEventListener("load", this.onPageLoad, true);
    var messagepane = document.getElementById("messagepane"); // mail
      messagepane.addEventListener("load", this.onPageLoad, true);

  onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
    // do something with the loaded page.
    // doc.location is a Location object (see below for a link).
    // You can use it to make your code executed on certain pages only.
    if("forum") > -1)
      alert("a forum page is loaded");


  • See Chris Neale's URIid extension for a working example.
  • If you need to have a more complicated listener (not just onload), use progress listeners.
  • Location object