Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - AfterDocumentReady?

Tags:

jquery

Is there an event that I can register that will be executed after all $(document).ready scripts are executed? I've got a piece of code that needs to be executed after the whole page is fully loaded (also the jQuery scripts). Is this possible?

I need to register this in a script file, so can't put the function at the bottom of the page.

like image 575
Kees C. Bakker Avatar asked Aug 05 '11 09:08

Kees C. Bakker


People also ask

What comes after document ready jQuery?

So, there is no event called after document. ready(). You'll need to create and wait for events to complete on your own, or use window. load().

Is jQuery document ready deprecated?

on( "ready", handler ) , deprecated as of jQuery 1.8 and removed in jQuery 3.0.

How does jQuery document ready work?

The jQuery document ready function executes when the DOM (Document Object Model) is completely loaded in the browser. jQuery document ready is used to initialize jQuery/JavaScript code after the DOM is ready, and is used most times when working with jQuery. The Javascript/jQuery code inside the $(document).

Does document ready wait for scripts?

The document. ready() function will be executed as soon as the DOM is loaded. It will not wait for the resources like images, scripts, objects, iframes, etc to get loaded.


1 Answers

It is not possible to attach an event to when all of the scripts are executed and run. There is no hook for for your particular case in javascript because scripts can have very different logic like being loaded asynchronously, having timeouts, etc. But there is an event that is fired when all of the scripts are loaded in the browser (but still they are not executed):

$(window).load(function () {
    // your code is here
});

will be executed after the whole page including scripts and graphics will be loaded. Though, this doesn't mean the scripts are actually executed. But, if your scripts only contain the logic that requires immediate actions like

alert('Hi!');

then those will be executed before $(window).load()

like image 154
spliter Avatar answered Oct 11 '22 12:10

spliter