views:

800

answers:

6

I want to write an application that detects when a page is loaded in the browser, then I should be able to insert content on top of the loaded web page? Anybody with an idea on how to do that?

Please note that I should be able to do this in any browser (Firefox/IE).

What language should I use to help me do this?

How do I detect this from an external application?

How should I integrate this with the browser?

+2  A: 

In Javascript, you have the onload event.

Edit: an example:

<html>
  <head>...</head>
  <body onload="doSomethingWhenPageIsLoaded();">
    ...
  </body>
</html>
romaintaz
-1 for adding JavaScript to HTML. Better to externalize it completely.
Ryan Doherty
A: 

Javascript's OnLoad event for the body does what you want.

<body onload="...">
Welbog
+2  A: 

Javascript using the onLoad() event, will wait for the page to be loaded before executing.

<body onload="somecode();" >

If you're using the jQuery framework's document ready function the code will load as soon as the DOM is loaded and before the page contents are loaded:

$(document).ready(function() {
    // jQuery code goes here
});
jjclarkson
+1 for mentioning more than one way
meouw
Good response. However, if the poster doesn't know basic Javascript, I doubt he knows jQuery.
Zabbala
+7  A: 

You would use javascript to do this. If you don't know how to use javascript, I would recommend reading through some tutorials first.

After you have a basic understanding of javascript, you can detect when a page has loaded by using the window.onload event.

window.onload = function() {
  addPageContents();  //example function call.
}

Edit: If you want to add more than one onload function, and not use a javascript library, you can wrap your own onload hanlder.

window.whenloaded = function(fn) {
  if (window.onload) {
    var old = window.onload;
    window.onload = function() {
      old();
      fn();
    }
  } else {
    window.onload = fn;
  }
}
tj111
+1 because you didn't say 'just use jQuery'
meouw
ditto. It's getting beyond a joke in here. "How do I assign a value to a variable?" "Use jQuery!"
bobince
This method will remove any previously attached event handlers. If you used jQuery instead, this wouldn't happen.
Chase Seibert
@Chase, see the edit.
tj111
Why not use the native `addEventListeners` method?
Luca Matteis
+11  A: 

Better than onload is to use a function of an existing framework, because onload does sometimes respond after all the resources (images and so on) are loaded and not only the page.

For example jQuery:

$(document).ready( function() {
    // do stuff
}
Georg
+1 because you explain that libraries have a better onload
meouw
+1 , this is 10x better than the onload event for the precise reasons you mention. Too many people use onload and it might not fire for seconds after the page is displayed to the user.
Ryan Doherty
This also has the advantage of playing nice with existing event handlers. window.onload on the other hand will over-write them.
Chase Seibert
Not that you actually require a library for this. Dean Edwards has a good discussion on this: http://dean.edwards.name/weblog/2005/09/busted/ (and an update)
annakata
+1  A: 

Why not use listeners?

// Everything but IE
window.addEventListener("load", function() {
    // loaded
}, false); 

// IE
window.attachEvent("onload", function() {
    // loaded
});

This way you can add as many Listeners as you want, you can also detach them! removeEventListener and detachEvent.

Luca Matteis