Results 1 to 3 of 3

Thread: window.onload vrs <body onload="">

  1. #1
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default window.onload vrs <body onload="">

    Hi,

    I have a div element that contains some text. I need to know the height of this text before the page is displayed and I'm trying to find the quickest method possible.

    1. Is body onload faster then using window.onload?
    2. Do these events get executed before any of the page is displayed?
    3. Can you use body onload in a external javascript file instead of in the <body> tag?

    The reason I'm wondering is I don't want the elements to 'flicker' on the page after I set style properties using javascript. The page should display properly immediately without any changes.

    Thanks
    Last edited by Cyclone; 03-16-2009 at 10:14 AM.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by Cyclone View Post
    1. Is body onload faster then using window.onload?
    I don't know, although I would guess that 'body onload' would be faster.
    Quote Originally Posted by Cyclone View Post
    2. Do these events get executed before any of the page is displayed?
    No, since the page has to load first.
    Quote Originally Posted by Cyclone View Post
    3. Can you use body onload in a external javascript file instead of in the <body> tag?
    Yes, since
    Code:
    <script type="text/javascript">
    function bla(){
    ...do-something...
    ]
    </script>
    ...
    <body onload="bla()">
    can be replaced with:
    Code:
    <script type="text/javascript">
    function bla(){
    ...do-something...
    ]
    window.onload=bla;
    </script>
    which, in turn, can be put in an external js-file.
    ===
    Arie.

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    1. Is body onload faster then using window.onload?
    They have just about the same speed.

    2. Do these events get executed before any of the page is displayed?
    As molendijk said, no. They execute after the loading of the page and its elements. For example, if you have a large image that is taking a while to load, it will execute after that image loads.

    3. Can you use body onload in a external javascript file instead of in the <body> tag?
    As molendijk said, yes. Simply take the window.onload = function() { /* code here */ }; and place it into the external file. Note that you can not use window.onload and <body onload=""> simultaneously.

    I have a div element that contains some text. I need to know the height of this text before the page is displayed and I'm trying to find the quickest method possible.
    It's actually impossible, considering that the page must load before you can do anything with the elements on the page. See, you can execute javascript before the page even loads, though it is not allowed to retrieve elements. The elements have not loaded then, thus you cannot access them because they do not exist. In turn, it will have to flicker if you want your javascript to occur. Questions?

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
  •