Take the 2-minute tour ×

i have a javascript that i need to load at the body, but since im trying to build something flexible i really want it to function on a onLoad on a divtag. But thats not going to happen as i understand it as onLoad does not work on a divtag.

is there another way i can load/start a function within a divtag? or when i include the .js file?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

you can embed script snippets which execute when they are loaded. This example is from jquery...

<span class="myclass otherclass">span class="myClass"</span>
<script>$(".myclass.otherclass").css("border","13px solid red");</script>

Maybe you need to make your question more clear?

share|improve this answer
That's a reasonable idea. You'd have to run it after the element it references has loaded in the DOM though. –  Diodeus May 13 '10 at 0:51
i really wanted to be independent of another lib but jquery solves my problem. –  Jason94 May 13 '10 at 1:00
While many would not consider this a replacement for body onload, you can make it work. jQuery does solve so many problems though. It reduces the complexity of a problem by three-to five times. –  Diodeus May 13 '10 at 2:06
add comment

You can use a timer to check for a change. Not very efficient, but it will work.

That begs the question: What is loading into the DIV? How did it start? Where did it come from? Perhaps these are more worthy of investigation.

If you force the "source" that is changing the element into changing something that has an onchange event, you can certainly trap that. Perhaps routing an intended DIV change to an INPUT could solve the problem.

share|improve this answer
Hmm... starting a timer would be silly because it will be a hack. how does lightbox do it? it does something like this: <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script> –  Jason94 May 13 '10 at 0:39
Scripatculous is a well-known library. Prototype, which it is built upon, can add elements to the DOM as well as events. It could be making an AJAX call. –  Diodeus May 13 '10 at 0:42
but what does "?load=effects,builder" do? pass arguments? maybe i can use it to fire up my Init function in my .js file? –  Jason94 May 13 '10 at 0:44
It's simply loading the library with options. There is probably another script somewhere that calls the "Effect" object in Scriptaculous, look for it (case sensitive). More info: script.aculo.us –  Diodeus May 13 '10 at 0:48
add comment

Your Answer


