views:

27

answers:

4
<div onload="oQuickReply.swap();" ></div>

Can i use for this?

+1  A: 

The onload event can only be used on the document itself (body), frames, images, and scripts. In other words, it attaches to the body plus each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.

kijin
A: 

I really like the YUI3 library for this sort of thing.

<div id="mydiv"> ... </div>

<script>
YUI().use('node-base', function(Y) {
  Y.on("available", someFunction, '#mydiv')
})

See: http://developer.yahoo.com/yui/3/event/#onavailable

mjhm
That's quite a large blob of JS to dump onto the page for the sake of binding a single `onload`.
meagar
can i will use for normal not framework?
monkey_boys
@meagar -- Right, though I'm finding it increasingly rare that I do just two or three simple JS things on a page. Worrying about cross browser compatibility also drives me nuts.
mjhm
A: 

Use the body.onload event instead, either via attribute (<body onload="myFn()"> ...) or by binding an event in Javascript. This is extremely common with jQuery:

$(document).ready(function() {
    doSomething($('#myDiv'));
});
mway
A: 

No, you can't. The easiest way to make it work would be to put the function call directly after the element

Example:

...
<div id="somid">Some content</div>
<script>oQuickReply.swap('somid');</script>
...

or - even better - just in front of </body>

...
<script>oQuickReply.swap('somid');</script>
</body>
DanMan