Clearing jquery call document.ready ()

How to clear anonymous functions that are triggered when jquery calls document.ready ()?

For instance:

<script type="text/javascript"> //some code sets a doc ready callback $(document).ready(function () { alert('ready'); }); //my attempt to prevent the callback from happening window.onload = null; $(document).unbind("ready"); </script> 

A warning occurs regardless of my attempts to get around it. Is there any way to do this?

+6
source share
4 answers

You will probably get the most appropriate answer if you described what problem you are really trying to solve.

jQuery does not have a public document to cancel or block document.ready () handlers. If you control the code, you can use a global variable and a condition like this:

 var skipReady = false; $(document).ready(function () { if (!skipReady) { alert('ready'); } }); // skip the document.ready code, if it hasn't already fired skipReady = true; 

Or, if you want to hack jQuery a bit (outside of the documented interfaces), you can do this:

 $(document).ready(function() { alert("ready"); }); // stop the ready handler $.isReady = true; 

You can see this latest work here: http://jsfiddle.net/jfriend00/ZjH2k/ . This works because jQuery uses the: $.isReady property to keep track of whether they have already fired ready-made handlers or not. By setting it to true, he thinks that he has already fired them, so that he does not do it again every time.

+5
source

It works:

 $(document).bind("ready", function () { alert("hey!"); }); $(document).unbind("ready"); 

It seems to me that the error is that all other events in jQuery may be unrelated. Omission of this discrepancy.

Not a direct answer to the omission, but here is some related information from jQuery docs :

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)
  • $().ready(handler) (not recommended)
  • $(handler)

There is also $(document).bind("ready", handler) . This behaves similarly to a ready-made method, but with one exception: if a ready-made event is already running and you try to execute .bind("ready") , the binding handler will not be executed. Ready handlers linked in this way are executed after any binding by the three other methods above.

+1
source

$ (document) .ready () depends on the onLoad event that the browser fires, which means you cannot prevent it. If alert () is defined by some condition, then I would use the if / else statement to decide if it would be called.

0
source

A super old question, but I came across the need to do this recently to prevent document.ready code, which I did not control due to launch in certain cases. This can be achieved by proxying the jQuery ready function, more like a test spy. The following will work:

 var ready = $.prototype.ready; // proxy the ready function $.prototype.ready = function ( fn, allowed ) { allowed = allowed || false; if ( allowed ) { ready.call( this, fn ); } }; 

All calls to $( document ).ready will now be ignored. You can override this behavior by passing true as the second argument: $( document ).ready( fn, true )

0
source

Source: https://habr.com/ru/post/899565/


All Articles