6

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

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

0

4 Answers 4

23

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

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

or even more simply, just pass the method to jQuery:

$(callFact);

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

Sign up to request clarification or add additional context in comments.

Comments

7

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').

2 Comments

IFRAME fire onload event too... ;-)
True, and generally elements with an src attribute (such as object, applet, embed, script...) do.
4

You'll probably want to do this instead:

$('#body').ready(function(){
    calFact();
});

1 Comment

gnarf's answer is definitely best if your calFact() is not targeted to a specific DOM element as well. :)
3

You should use ready()

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

2 Comments

Thank you very much for all answers. Invoking function in document ready works !!!
Actually all the answers were true :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.