tags:

views:

50

answers:

2

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?

+1  A: 

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.

Diodeus
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
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
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
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: http://script.aculo.us/
Diodeus
+1  A: 

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?

Jacob
That's a reasonable idea. You'd have to run it after the element it references has loaded in the DOM though.
Diodeus
i really wanted to be independent of another lib but jquery solves my problem.
Jason94
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