2

I'm trying to write a very simple user content script for google.com, but the problem is that google's source code is lengthy. I want to execute code in javascript the instant that an element is in the document, but before the whole document has loaded.

Essentially, I wan't to change the source of an image before the rest of the page loads. I also want to modify html in a certain other div with a specific id. But again, I don't want to wait for the rest of the document to load before I start doing it.

How can I accomplish this? I am using jquery.

jackcogdill
  • 4,900
  • 3
  • 30
  • 48

1 Answers1

-1

Try this:

var element = $([TagName]) || document.getElementsByTagName([TagName])[[occurance]];

function doSomething() {
  // Do some magic.
}

element.addEventListener("load", doSomething, false); // Notice no brackets after the function call.

This adds an event listener to the element, and will wait until it has fully loaded until it runs the function (doSomething()).

Hope this helps.

gilbert-v
  • 1,263
  • 1
  • 11
  • 23