On Page Load

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 03:16, 19 December 2004
Asqueella (Talk | contribs)

<-- Previous diff
Revision as of 10:56, 16 January 2005
Asqueella (Talk | contribs)
(On Tab Load)
Next diff -->
Line 35: Line 35:
[ Location object] [ Location object]
-[[Category:Development]]+[[Category:Development|On Tab Load]] [[Category:Example code|On Tab Load]]

Revision as of 10:56, 16 January 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 page is for people who want to have their code executed each time a new page is loaded in browser/mail.

You need to create an overlay to chrome://browser/content/browser.xul (Firefox), chrome://navigator/content/navigator.xul (browser - Mozilla Suite) or chrome://messenger/content/messenger.xul (Thunderbird and Mozilla Mail) and add a load event listener to appcontent element (browsers) or messagepane (mail):

window.addEventListener('load',  onInit, false);

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

function onPageLoad(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");

Of course, you need to use unique identifiers instead of generic onInit and onPageLoad.

See cdn's URIid extension for a working example.

If you need to have a more complicated listener (not just onload), use progress listeners.


Location object