tags:

views:

50

answers:

4

HI,

I m using the following script to call a onload function but it does not works in IE

("#body").attr({onload : "calFact();"});

+3  A: 

If you are using jQuery you could use the ready() function like so:

$(function() {
  callFact();
});

That will fire as soon as the DOM is available to manipulate, sometimes earlier than onload.

gnarf
A: 

You should use ready()

More at http://api.jquery.com/ready/

Rahul
Thank you very much for all answers.Invoking function in document ready works !!!
then you should mark this solution as the answer ;)
Rahul
Actually all the answers were true :)
A: 

You'll probably want to do this instead:

$('#body').ready(function(){
    calFact();
});
Richard Neil Ilagan
gnarf's answer is definitely best if your calFact() is not targeted to a specific DOM element as well. :)
Richard Neil Ilagan
+1  A: 

You cannot set onload event handlers on anything other than the window. (Well, you can, but it will have no effect.) If you want calFact to run at the onload event, you should use $(window).load(calFact);. But generally it is more useful to call things at the DOMready event: $(document).ready(calFact); or just $(calFact); for short. The difference is that onload happens when everything loaded (including images, which are slow to load), and DOMready happens when the DOM tree is loaded (but images not necessarily yet).

Also, there is not much point in assigning an id of body to the body when there is only one of it anyway; you can just use $('body').

Tgr
Images fire `onload` for usefulness as well...
gnarf
IFRAME fire onload event too... ;-)
Nordin
True, and generally elements with an `src` attribute (such as `object`, `applet`, `embed`, `script`...) do.
Tgr