Execute Function After Complete Page Load

call function after complete page load

Most likely you're getting a value of 0 because you're printing the information before the page has completed loading, try the following snippet:

window.onload = function(){
setTimeout(function(){
var t = performance.timing;
console.log(t.loadEventEnd - t.responseEnd);
}, 0);
}

That will make sure the numbers are printed after the page is actually ready. Got that snippet from here. Other information from that page you might find interesting:

Data from the API really comes to life when events are used in combination:

  • Network latency (): responseEnd-fetchStart.
  • The time taken for page load once the page is received from the server: loadEventEnd-responseEnd.
  • The whole process of navigation and page load: loadEventEnd-navigationStart.

How to make JavaScript execute after page load?

These solutions will work:

As mentioned in comments use defer:

<script src="deferMe.js" defer></script>

or

<body onload="script();">

or

document.onload = function ...

or even

window.onload = function ...

Note that the last option is a better way to go since it is unobstrusive and is considered more standard.

Execute JS function after some time of page load

Use setTimeout instead, as it is called only once after the pause:

setTimeout(myFunc, 3000);

How to execute a function when page has fully loaded?

That's called load. It came waaaaay before DOM ready was around, and DOM ready was actually created for the exact reason that load waited on images.

window.addEventListener('load', function () {
alert("It's loaded!")
})

How do I call a JavaScript function on page load?

If you want the onload method to take parameters, you can do something similar to this:

window.onload = function() {
yourFunction(param1, param2);
};

This binds onload to an anonymous function, that when invoked, will run your desired function, with whatever parameters you give it. And, of course, you can run more than one function from inside the anonymous function.

How to call a function after Complete page load and after all external script execution?

I have been terribly interested with your question and going deep to the jQuery source I came up with a mad hack :)

But the key point is that you should put this piece of code at the very beginning, right after you plug jQuery:

$.statesNum = 0;
$.fn.ready = function ( fn ) {
$.statesNum++;
jQuery.ready.promise().done( fn ).then(function () {
$.statesNum--;
if ($.statesNum == 0) {
$(document).trigger("afterReady");
}
});

return this;
};

Now whenever you want to execute something after all .ready functions are done you can do like this:

$(document).on("afterReady", function () {
alert("Hey, the ready functions are executed");
});

execute a javascript after page load is complete

If you use jquery, the below code might work:

$(document).ready(function() {
$.getScript("http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907");
});

Wihtout Jquery

window.onload = function() {
var element = document.createElement("script");
element.src = "http://bdv.bidvertiser.com/BidVertiser.dbm?pid=503589&bid=1747907";
document.getElementsByTagName("head")[0].appendChild(element );
}

How to run a function when the page is loaded?

window.onload = codeAddress; should work - here's a demo, and the full code:

<!DOCTYPE html><html>    <head>        <title>Test</title>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <script type="text/javascript">        function codeAddress() {            alert('ok');        }        window.onload = codeAddress;        </script>    </head>    <body>        </body></html>

Javascript Run Function After Page Load

This code has several issues:

if (addEventListener in document) { // use W3C standard method
document.addEventListener('load', meerfirst(), false);
} else { // fall back to traditional method
document.onload = meerfirst();
}
  1. You need to put quotes around addEventListener in the if statement. You're looking for a property name in document, so the name needs to be a string: if( 'addEventListener' in document )

  2. You want to refer to the function by name in the addEventListener statement, not call it immediately. You should remove the parentheses - use just meerfirst instead of meerfirst().

  3. Assigning to document.onload has no effect. See my answer to window.onload vs document.onload and use window.onload instead.

  4. After changing the assignment to window.onload you also need to remove the parentheses. Again, you're just referring to the function by name and don't want it to actually be called at this point.

  5. Finally, I recommend listening for DOMContentLoaded rather than load - unless you are going to run some code that needs to wait until all images are loaded.



Related Topics



Leave a reply



Submit