Jump to content

[SOLVED] can't understand this


qbox

Recommended Posts

Hi to all

I read on internet one script and look like this

 

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

 

var myExtension = {

  init: function() {

    var appcontent = document.getElementById("appcontent");  // browser

    if(appcontent)

      appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);

    var messagepane = document.getElementById("messagepane"); // mail

    if(messagepane)

      messagepane.addEventListener("load", function () { myExtension.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(doc.location.href.search("forum") > -1)

      alert("a forum page is loaded");

  }

 

from http://developer.mozilla.org/en/docs/Code_snippets:On_page_load

I can't understand this code. What is the function? What this do? Why is in this way?

What is that init line and what he do?

Thank you.

Link to comment
Share on other sites

From what I understand, you attach that to the UI of Firefox, and it will execute everytime a page loads.

 

For example, the addEventListener call is simply saying "make it so when ever the window loads the functino myExtension.init() is run."

 

Then, myExtension is simply a variable with a class in it, and init is a method.  Then onPageLoad simply sets something to be done when the page loads (In this case, if the URL contains "forum" in it, an alert box will popup and say "a forum page is loaded").

Link to comment
Share on other sites

Like I understand with

 

init: function{

......

OnLoad();

}

 

OnLoad(){

...

}

 

with init function I can call OnLoad function to execute without to indicate in body tag, and I can set some var values who are already set before without indicating again.

Im right or not?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.