Results 1 to 3 of 3

Thread: calling a function on body onload

  1. #1
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default calling a function on body onload

    how i can cal a function on body onload?

    i dont want to use <body onload="func()">

    what i the alternative for this coding?

    the script is also defined within the page.....

    but need to call that functions on body loading.....

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Are you using jQuery? If so:

    Code:
    <script type="text/javascript">
    jQuery(window).load(func);
    </script>
    Or if document ready which happens sooner is OK:

    Code:
    <script type="text/javascript">
    jQuery(function($){
    	func();
    });
    </script>
    Regular javascript:

    Code:
    <script type="text/javascript">
    if (window.addEventListener){
    	window.addEventListener('load', func, false);
    }
    else if (window.attachEvent){
    	window.attachEvent('onload', func);
    }
    </script>
    There's also:

    Code:
    <script type="text/javascript">
    window.onload = func;
    </script>
    But that has almost as many problems as the <body onload="func();"> method does.

    Or if you want to use document ready, place this before the closing </body> tag:

    Code:
    <script type="text/javascript">
    func();
    </script>
    Aside from the last one (document ready, regular javascript), these may all go in the head or wherever pretty much and their code may go in an external file that's called, as long as they come after func() is defined.

    There are variations on at least some of these methods, probably even other methods. But these should give you enough options to work with for now. If you have something in particular in mind and none of these seems ideal, let me know what the circumstances are.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    thanx a lot dear .......................

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •