Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:15:19+00:00 2026-05-27T21:15:19+00:00

I was wondering if the window.onload = function(){} (or any other kind of onload,

  • 0

I was wondering if the window.onload = function(){} (or any other kind of onload, like the jQuery $(document).ready(); is necessary if the code is placed at the bottom of my <body>?

Or there could be highly unexpected side-effects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:15:20+00:00Added an answer on May 27, 2026 at 9:15 pm

    Yes, there could be unexpected consequences. But, no, it’s not absolutely necessary. The timing could be off for things still loading, like complicated layouts, deep DOM structures, dynamic HTML from other scripts, or images. To avoid these situations, it’s always safest to wrap your script in an onload event.

    Here are some examples that demonstrate this. All examples tested on Chrome 17.0.963.12 dev on OS X. Browser results may vary when not using onload, which demonstrates its unpredictable behavior. The examples return fail if the result is different than what you’d expect (i.e. what your design specifies) and return success when the result matches what you would expect. With onload they always return success.

    Example 1

    In this example, the code is expecting the image to be a certain width. If the code is wrapped in an onload event the width is correct, otherwise, it’s not.

    Demo: http://jsfiddle.net/ThinkingStiff/qUWxX/

    HTML:

    <div id="result"></div>
    <img id='image' src="http://thinkingstiff.com/images/matt.jpg" />
    

    Script:

    document.getElementById( 'result' ).innerHTML 
        = document.getElementById( 'image' ).offsetWidth == 346 ? 'success': 'fail';
    

    You’ll see the jsFiddle is set to “onLoad” in the upper left corner of the page and the result above the image is success.

    enter image description here

    Change that to “onDomReady” or “no wrap (body)”:

    enter image description hereenter image description here

    Now press “Run” at the top left of the page:

    enter image description here

    The result above the image will now be fail.

    Example 2

    Here is another example that doesn’t use images. In this one, an inline script has been added to the HTML. The code is expecting the width to be what it was set to by the inline script. With onload it’s corrent, without, it’s not. Use the same instructions as before for this demo.

    Demo: http://jsfiddle.net/ThinkingStiff/n7GWt/

    HTML:

    <div id="result"></div>
    <div id="style"></div>
    
    <script>
        window.setTimeout( function() { 
            document.getElementById( 'style' ).style.width = '100px'; 
        }, 1 );
    </script>
    

    Script:

    document.getElementById( 'result' ).innerHTML 
        = document.getElementById( 'style' ).style.width ? 'success' : 'fail';
    

    Example 3

    Here’s an example that uses no images or Javascript in the body, just CSS. Again, the results are different between onload and not.

    Demo: http://jsfiddle.net/ThinkingStiff/HN2bH/

    CSS:

    #style {
        animation:             style 5s infinite;
            -moz-animation:    style 5s infinite;
            -ms-animation:     style 5s infinite;
            -o-animation:      style 5s infinite;
            -webkit-animation: style 5s infinite;
        border: 1px solid black;
        height: 20px;
        width: 100px;    
    }
    
    @keyframes             style { 0% { width: 100px; } 100% { width: 500px; } }
        @-moz-keyframes    style { 0% { width: 100px; } 100% { width: 500px; } }
        @-ms-keyframes     style { 0% { width: 100px; } 100% { width: 500px; } }
        @-o-keyframes      style { 0% { width: 100px; } 100% { width: 500px; } }
        @-webkit-keyframes style { 0% { width: 100px; } 100% { width: 500px; } }
    

    HTML:

    <div id="result"></div>
    <div id="style"></div>
    

    Script:

    document.getElementById( 'result' ).innerHTML 
        = document.getElementById( 'style' ).clientWidth > 100 ? 'success' : 'fail';
    

    There are just too many scenarios where not wrapping your code can cause issues that you won’t be able to anticipate. To avoid these situations, it’s always safest to wrap your script in an onload event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if my jquery function is correct <script> window.blinker = setInterval(function(){ if(window.alerta){
I was just wondering if there are any advantages of using jquery $(window).width(); $(window).height();
Just wondering why my code isn't working. Am I doing this completely wrong? window.onload
I am wondering how Internet Explorer, Mozilla Firefox or any other browser generate print
I was wondering if it's possible to write something like this: <Window ... xmlns
I'm wondering if there is any case where window.location will not work? I was
I'm starting to code up my own window manager, and was wondering how to
I'm struggling to make the code blow work... Then, I'm wondering why window.focus() and
window.onload = init; function init(){ initializeEventHandlers(); getData(formatDate(new Date)); gotoDate('today'); } //scripts that manage the
I have this jQuery slide show from JQuery Cycle plugin.... <script type=text/javascript> $(window).on('load', function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.