If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
I want to do javascript code only after the page contents are loaded how can I do that?
If you want an event to work on your page, you should call it inside the $(document). ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
You can insert that code in your jQuery function IMO: $("foo"). click(function() { window. location="www.google.com"; });
The load event occurs when a specified element has been loaded. This event works with elements associated with a URL (image, script, frame, iframe), and the window object. Depending on the browser, the load event may not trigger if the image is cached (Firefox and IE).
Use load
instead of ready
:
$(document).load(function () { // code here });
Update You need to use .on()
since jQuery 1.8. (http://api.jquery.com/on/)
$(window).on('load', function() { // code here });
From this answer:
According to http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/:
Removed deprecated event aliases
.load
,.unload
, and.error
, deprecated since jQuery 1.8, are no more. Use.on()
to register listeners.https://github.com/jquery/jquery/issues/2286
Following
$(document).ready(function() { });
can be replaced
$(window).bind("load", function() { // insert your code here });
There is one more way which I'm using to increase the page load time.
$(document).ready(function() { $(window).load(function() { //insert all your ajax callback code here. //Which will run only after page is fully loaded in background. }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With