On Page Load: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
 
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This page is for people who want to have their code executed each time a new page is loaded in browser/mail.
#redirect [[On page load]]
 
You need to create an overlay to <tt>chrome://browser/content/browser.xul</tt> (Firefox), <tt>chrome://navigator/content/navigator.xul</tt> (browser - Mozilla Suite) or <tt>chrome://messenger/content/messenger.xul</tt> (Thunderbird and Mozilla Mail) and add a ''load'' event listener to ''appcontent'' element (browsers) or ''messagepane'' (mail):
 
<pre>window.addEventListener('load',  onInit, false);
 
function onInit() {
  var appcontent = document.getElementById("appcontent");  // browser
  if(appcontent)
    appcontent.addEventListener("load", onPageLoad, true);
  var messagepane = document.getElementById("messagepane"); // mail
  if(messagepane)
    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(doc.location.href.search("forum") > -1)
    alert("a forum page is loaded");
}
</pre>
 
Of course, you need to use [[Dev : Javascript coding guidelines|unique identifiers]] instead of generic ''onInit'' and ''onPageLoad''.
 
See cdn's URIid extension for a working example.
 
===References===
[http://xulplanet.com/references/objref/Location.html Location object]

Latest revision as of 20:26, 6 December 2005

Redirect to: