Second $ (document) .ready jQuery event

I am using some kind of external jQuery with $ (document) .ready () to insert advertisements after triggering a document event, for example:

$(document).ready( function() { $('#leaderboard').html("<strong>ad code</strong>"); }); 

This is to prevent the user interface from being blocked by slow loading of advertisements. So far, it works well.

Now I need to add a few more declarations, although our CMS system, it can not be part of an external JS file, so I wonder if I can use the event for the second document and insert it using the built-in script tag? If so, what will be the execution order of the external event of the JS document, or the first script?

+6
javascript jquery html blocking advertising
source share
5 answers

You can use as many event methods as you want, jquery attaches them to the queue. The method call order is the same as the order of determination - the last added last is called.

A useful thing may be that you can load the html code with a script using ajax and when the code loads in DOM $ (). The ready () function will also be called so that you can dynamically load ads.

+10
source share

Yes, adding a few $ (documents) .ready () s is not a problem. Everything will be done at the finished event.

Please note that your sample code is incorrect. $ (document) .ready () takes a function, not an expression. Thus, you should give it such a function:

  $(document).ready( function() { $('#leaderboard').html("<strong>ad code</strong>"); }); 

This function will be executed when the document is ready.

+9
source share

Here is a short tutorial on preparing several documents

+6
source share

An additional bonus of the jQuery method is that you can have several ready-made () definitions. This applies to all jQuery events.

$ (document) .ready (function () {alert ("Number One");});

$ (document) .ready (function () {alert ("Number Two");

+3
source share

JQuery calls ready-made functions in the order in which they are defined. If you want to load some data first and do the execution, holdReady () .

+1
source share

All Articles