173

I am using following code to execute some statements after page load.

 <script type="text/javascript">
    window.onload = function () { 

        newInvite();
        document.ag.src="b.jpg";
    }
</script>

But this code does not work properly. The function is called even if some images or elements are loading. What I want is to call the function the the page is loaded completely.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Neeraj Kumar
  • 1,873
  • 2
  • 14
  • 10
  • 1
    Could you add a demo on http://JSFiddle.net? – Some Guy Aug 13 '12 at 14:53
  • 2
    Could you use jQuery? If so, try `$(window).load()` – Matt Hintzke Aug 13 '12 at 14:55
  • 1
    Yes, could you explain exactly what isn't working properly? Are you getting an error, or perhaps you're calling an `alert` function before the window redraws (making it look like it's called before load)? The code looks fine, and forcing users to download large libraries probably won't fix this issue or make it any easier to diagnose. – Jeffrey Sweeney Aug 13 '12 at 15:06
  • http://www.doxdesk.com/img/updates/20091116-so-large.gif – Jeffrey Sweeney Aug 13 '12 at 15:09
  • When i say it isnt working, i mean than the function is fired even if some images are being loaded. – Neeraj Kumar Aug 13 '12 at 15:30
  • 1
    How do you determine that the image isn't loaded? I usually test by checking to see if the image's `width` is greater than 0. – Jeffrey Sweeney Aug 13 '12 at 15:46
  • I recommend using .load () with element is works fine! See your code where you end the element to put your `$(element).load (function () { ... });`. In fact it does not work from using `$(document)` and `$(windows)` are unfortunately not 100%. – KingRider Sep 08 '17 at 11:38
  • @NeerajKumar As far as I know `window.onload` waits for image loads too. I see the question is from 2012, maybe old browsers worked differently, I am not sure. If backward compatibility is important to you, then you can always add a script, which checks whether your images are loaded before you continue. – inf3rno Sep 02 '18 at 00:31
  • This Q&A is a mystery to me. The question is still not clear (e.g. how the problem manifests itself), there’s no [mre] provided, no debugging (e.g. Network tab in the browser’s dev tools) available — yet it has 0 downvotes? The `load` event will wait for all resources to be fully loaded if their load is initiated at site load. If you load a resource _after_ that, e.g. with your `document.ag.src="b.jpg";` line, then you’ll need a separate `load` listener, e.g. `document.ag.addEventListener("load",`…`);`. The top voted answer suggests `DOMContentLoaded` which isn’t a solution to this _at all_. – Sebastian Simon Sep 04 '20 at 19:08
  • 1
    Does this answer your question? [How to make JavaScript execute after page load?](https://stackoverflow.com/questions/807878/how-to-make-javascript-execute-after-page-load) – T.Todua Feb 09 '21 at 21:08

16 Answers16

211

this may work for you :

document.addEventListener('DOMContentLoaded', function() {
   // your code here
}, false);

or if your comfort with jquery,

$(document).ready(function(){
// your code
});

$(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers. This is why jQuery will most probably implement some heavy workarounds to support all the browsers. And this will make it very difficult to "exactly" simulate the behavior using plain Javascript (but not impossible of course).

as Jeffrey Sweeney and J Torres suggested, i think its better to have a setTimeout function, before firing the function like below :

setTimeout(function(){
 //your code here
}, 3000);
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Shreedhar
  • 5,502
  • 3
  • 22
  • 27
  • 16
    jQuery's `ready` will not do what the OP requested. `ready` fires when the DOM loads, not after elements load. `load` will fire after the elements finish loading/rendering. – Jaime Torres Aug 13 '12 at 15:02
  • 1
    It looks like the OP wants all the elements to be loaded entirely, not just the HTML. – Jeffrey Sweeney Aug 13 '12 at 15:03
  • then he could need to write some delay before firing the function. – Shreedhar Aug 13 '12 at 15:06
  • 3
    @shreedhar or he could use the right tool for the job (`load`). – Jaime Torres Aug 13 '12 at 15:42
  • 21
    `setTimeout` is a bad idea. It relies on the page loading under 3 seconds (or *n* seconds depending on what value you choose.) If loading takes longer, it won't work, and if the page loads faster, it'll have to wait for no reason. – JJJ Jul 04 '16 at 12:09
  • @Juhana yeah i almost agree with you, but for certain things which need not to be loaded during page render time, say google analytics. It can be loaded after n seconds what one can chose. – Shreedhar Jul 27 '16 at 17:56
  • Just for clarity... what is the false providing? I am assuming that after it fires it is setting the method to false – Christian Matthew Aug 25 '16 at 14:42
  • 2
    @ChristianMatthew its [useCapture](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTargets beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See [DOM Level 3 Events](https://www.w3.org/TR/DOM-Level-3-Events/#event-flow) for a detailed explanation. – Shreedhar Aug 31 '16 at 11:31
  • I needed to use `alert` so I wrapped it in `setTimeout` with value of `500` and then the `alert` didn't block any rendering – Robs Feb 07 '19 at 10:26
  • 1
    How is this the #1 answer? The `DOMContentLoaded` event will fire sooner than the `window.onload` event... – Ryan Walker Nov 07 '19 at 21:50
  • jQuery's `load` (for use as an event, anyway) has been removed from version 3 onward. What's the newer alternative? – GDorn Nov 15 '19 at 21:54
94

JavaScript

document.addEventListener('readystatechange', event => { 

    // When HTML/DOM elements are ready:
    if (event.target.readyState === "interactive") {   //does same as:  ..addEventListener("DOMContentLoaded"..
        alert("hi 1");
    }

    // When window loaded ( external resources are loaded too- `css`,`src`, etc...) 
    if (event.target.readyState === "complete") {
        alert("hi 2");
    }
});

same for jQuery:

$(document).ready(function() {   //same as: $(function() { 
     alert("hi 1");
});

$(window).load(function() {
     alert("hi 2");
});





NOTE: - Don't use the below markup ( because it overwrites other same-kind declarations ) :

document.onreadystatechange = ...
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
T.Todua
  • 53,146
  • 19
  • 236
  • 237
48

I'm little bit confuse that what you means by page load completed, "DOM Load" or "Content Load" as well? In a html page load can fire event after two type event.

  1. DOM load: Which ensure the entire DOM tree loaded start to end. But not ensure load the reference content. Suppose you added images by the img tags, so this event ensure that all the img loaded but no the images properly loaded or not. To get this event you should write following way:

    document.addEventListener('DOMContentLoaded', function() {
       // your code here
    }, false);
    

    Or using jQuery:

    $(document).ready(function(){
    // your code
    });
    
  2. After DOM and Content Load: Which indicate the the DOM and Content load as well. It will ensure not only img tag it will ensure also all images or other relative content loaded. To get this event you should write following way:

    window.addEventListener('load', function() {...})
    

    Or using jQuery:

    $(window).on('load', function() {
     console.log('All assets are loaded')
    })
    
Hanif
  • 3,739
  • 1
  • 12
  • 18
37

If you can use jQuery, look at load. You could then set your function to run after your element finishes loading.

For example, consider a page with a simple image:

<img src="book.png" alt="Book" id="book" />

The event handler can be bound to the image:

$('#book').load(function() {
  // Handler for .load() called.
});

If you need all elements on the current window to load, you can use

$(window).load(function () {
  // run code
});

If you cannot use jQuery, the plain Javascript code is essentially the same amount of (if not less) code:

window.onload = function() {
  // run code
};
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • 2
    Doesn't the jQuery `load` method just bind a function to the load event using Javascript? How would this be any different than what the OP is already doing? – Alex Kalicki Aug 13 '12 at 14:58
  • @AlexKalicki AFAIK it wouldn't be any different, except that jQuery might use `addEventListener` rather than bind the DOM0 `onload` property. – Alnitak Aug 13 '12 at 14:59
  • @AlexKalicki I cannot say that it uses the exact same functionality, but according to the documentation, jQuery is ensuring that all elements including images are loaded before this fires. I would tend to believe that there would be additional logic in place if this is the case, and I have no reason to disbelieve the documentation, although I have never run into the same issue the OP is reporting. – Jaime Torres Aug 13 '12 at 15:01
  • 4
    As of JQuery v1.8, .load is deprecated. [source](http://api.jquery.com/category/version/1.8/) – Adonis K. Kakoulidis Mar 12 '13 at 20:12
  • 1
    Best solution ever for the question. – Roberto Sepúlveda Bravo May 10 '17 at 23:49
  • @AdonisK.Kakoulidis The `load` event itself has not been deprecated. The standard way to attach jQuery events (`load` and others) is currently `.on( "load", handler )`, as described in the linked documentation, which still works. – Sebastian Simon Sep 04 '20 at 19:03
11

If you wanna call a js function in your html page use onload event. The onload event occurs when the user agent finishes loading a window or all frames within a FRAMESET. This attribute may be used with BODY and FRAMESET elements.

<body onload="callFunction();">
....
</body>
user2361682
  • 231
  • 1
  • 4
  • 7
  • Tried, didn't work. Using document.addEventListener('DOMContentLoaded', function() { // your code here }, false); works. – John T Nov 06 '20 at 00:55
11

You're best bet as far as I know is to use

window.addEventListener('load', function() {
    console.log('All assets loaded')
});

The #1 answer of using the DOMContentLoaded event is a step backwards since the DOM will load before all assets load.

Other answers recommend setTimeout which I would strongly oppose since it is completely subjective to the client's device performance and network connection speed. If someone is on a slow network and/or has a slow cpu, a page could take several to dozens of seconds to load, thus you could not predict how much time setTimeout will need.

As for readystatechange, it fires whenever readyState changes which according to MDN will still be before the load event.

Complete

The state indicates that the load event is about to fire.

Community
  • 1
  • 1
Ryan Walker
  • 715
  • 2
  • 10
  • 23
  • good but about the last paragraph: The `readystatechange = 'Complete'` is ok to assume whole page and contents (images, scripts, css) are all loaded. Although it is before `load` event but it doesn't matter here – S.Serpooshan Jan 07 '22 at 07:55
7

This way you can handle the both cases - if the page is already loaded or not:

document.onreadystatechange = function(){
    if (document.readyState === "complete") {
       myFunction();
    }
    else {
       window.onload = function () {
          myFunction();
       };
    };
}
Kejsi Struga
  • 550
  • 6
  • 21
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
7

you can try like this without using jquery

window.addEventListener("load", afterLoaded,false);
function afterLoaded(){
alert("after load")
}
NAA
  • 77
  • 1
  • 2
4
window.onload = () => {
  // run in onload
  setTimeout(() => {
    // onload finished.
    // and execute some code here like stat performance.
  }, 10)
}
tvrcgo
  • 319
  • 3
  • 4
  • 3
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Dec 26 '17 at 09:18
  • 1
    @yivi if that's not an automated message: it's pretty obvious to anyone what that code means, especially since there are comments in the code.. – B''H Bi'ezras -- Boruch Hashem Feb 21 '19 at 00:05
  • this works best to me. even if you remove the setTimeout block or just keep it there ;-) – Edang Jeorlie May 25 '23 at 12:16
4

Alternatively you can try below.

$(window).bind("load", function() { 
// code here });

This works in all the case. This will trigger only when the entire page is loaded.

Ganesh Ram Ravi
  • 169
  • 1
  • 10
1

I tend to use the following pattern to check for the document to complete loading. The function returns a Promise (if you need to support IE, include the polyfill) that resolves once the document completes loading. It uses setInterval underneath because a similar implementation with setTimeout could result in a very deep stack.

function getDocReadyPromise()
{
  function promiseDocReady(resolve)
  {
    function checkDocReady()
    {
      if (document.readyState === "complete")
      {
        clearInterval(intervalDocReady);
        resolve();
      }
    }
    var intervalDocReady = setInterval(checkDocReady, 10);
  }
  return new Promise(promiseDocReady);
}

Of course, if you don't have to support IE:

const getDocReadyPromise = () =>
{
  const promiseDocReady = (resolve) =>
  {
    const checkDocReady = () =>
      ((document.readyState === "complete") && (clearInterval(intervalDocReady) || resolve()));
    let intervalDocReady = setInterval(checkDocReady, 10);
  }
  return new Promise(promiseDocReady);
}

With that function, you can do the following:

getDocReadyPromise().then(whatIveBeenWaitingToDo);
Paul Rowe
  • 778
  • 3
  • 10
0

I can tell you that the best answer I found is to put a "driver" script just after the </body> command. It is the easiest and, probably, more universal than some of the solutions, above.

The plan: On my page is a table. I write the page with the table out to the browser, then sort it with JS. The user can resort it by clicking column headers.

After the table is ended a </tbody> command, and the body is ended, I use the following line to invoke the sorting JS to sort the table by column 3. I got the sorting script off of the web so it is not reproduced here. For at least the next year, you can see this in operation, including the JS, at static29.ILikeTheInternet.com. Click "here" at the bottom of the page. That will bring up another page with the table and scripts. You can see it put up the data then quickly sort it. I need to speed it up a little but the basics are there now.

</tbody></body><script type='text/javascript'>sortNum(3);</script></html>

MakerMikey

-1

If you're already using jQuery, you could try this:

$(window).bind("load", function() {
   // code here
});
Isaac
  • 11,409
  • 5
  • 33
  • 45
santosh
  • 799
  • 5
  • 17
-2

call a function after complete page load set time out

setTimeout(function() {
   var val = $('.GridStyle tr:nth-child(2) td:nth-child(4)').text();
 for(var i, j = 0; i = ddl2.options[j]; j++) {
  if(i.text == val) {      
   ddl2.selectedIndex = i.index;
   break;
  }
 } 
}, 1000);
M.H
  • 7
  • 1
-4

Try this jQuery:

$(function() {
 // Handler for .ready() called.
});
djserva
  • 395
  • 1
  • 11
  • 1
    jQuery's `ready` will not do what the OP requested. `ready` fires when the DOM loads, not after elements load. `load` will fire after the elements finish loading/rendering. – Jaime Torres Aug 13 '12 at 15:03
-6

Put your script after the completion of body tag...it works...

Boss
  • 445
  • 2
  • 8
  • 24