Create jQuery.holdReady(true/false) method to encapsulate jQuery.readyWait++ / jQuery.ready(true) logic. Fix problem where jQuery.ready may trigger twice, causing the (unsupported) document.onready to run twice. Fixes #8803 .
This commit is contained in:
parent
bbd9c776ea
commit
14193e449e
3 changed files with 24 additions and 18 deletions
18
src/core.js
18
src/core.js
|
@ -374,15 +374,19 @@ jQuery.extend({
|
||||||
// the ready event fires. See #6781
|
// the ready event fires. See #6781
|
||||||
readyWait: 1,
|
readyWait: 1,
|
||||||
|
|
||||||
|
// Hold (or release) the ready event
|
||||||
|
holdReady: function( hold ) {
|
||||||
|
if ( hold ) {
|
||||||
|
jQuery.readyWait++;
|
||||||
|
} else {
|
||||||
|
jQuery.ready( true );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Handle when the DOM is ready
|
// Handle when the DOM is ready
|
||||||
ready: function( wait ) {
|
ready: function( wait ) {
|
||||||
// A third-party is pushing the ready event forwards
|
// Either a released hold or an DOMready/load event and not yet ready
|
||||||
if ( wait === true ) {
|
if ( (wait === true && !--jQuery.readyWait) || (wait !== true && !jQuery.isReady) ) {
|
||||||
jQuery.readyWait--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure that the DOM is not already loaded
|
|
||||||
if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
|
|
||||||
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
|
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
|
||||||
if ( !document.body ) {
|
if ( !document.body ) {
|
||||||
return setTimeout( jQuery.ready, 1 );
|
return setTimeout( jQuery.ready, 1 );
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
// Simple script loader that uses jQuery.readyWait
|
// Simple script loader that uses jQuery.readyWait via jQuery.holdReady()
|
||||||
|
|
||||||
//Hold on jQuery!
|
//Hold on jQuery!
|
||||||
jQuery.readyWait++;
|
jQuery.holdReady(true);
|
||||||
|
|
||||||
var readyRegExp = /^(complete|loaded)$/;
|
var readyRegExp = /^(complete|loaded)$/;
|
||||||
|
|
||||||
function assetLoaded( evt ){
|
function assetLoaded( evt ){
|
||||||
var node = evt.currentTarget || evt.srcElement;
|
var node = evt.currentTarget || evt.srcElement;
|
||||||
if ( evt.type === "load" || readyRegExp.test(node.readyState) ) {
|
if ( evt.type === "load" || readyRegExp.test(node.readyState) ) {
|
||||||
jQuery.ready(true);
|
jQuery.holdReady(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<!--
|
<!--
|
||||||
Test for jQuery.readyWait. Needs to be a
|
Test for jQuery.holdReady. Needs to be a
|
||||||
standalone test since it deals with DOM
|
standalone test since it deals with DOM
|
||||||
ready.
|
ready.
|
||||||
-->
|
-->
|
||||||
<head>
|
<head>
|
||||||
<title>
|
<title>
|
||||||
jQuery.readyWait Test
|
jQuery.holdReady Test
|
||||||
</title>
|
</title>
|
||||||
<style>
|
<style>
|
||||||
div { margin-top: 10px; }
|
div { margin-top: 10px; }
|
||||||
|
@ -52,15 +52,17 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>
|
<h1>
|
||||||
jQuery.readyWait Test
|
jQuery.holdReady Test
|
||||||
</h1>
|
</h1>
|
||||||
<p>
|
<p>
|
||||||
This is a test page for jQuery.readyWait, that was
|
This is a test page for jQuery.readyWait and jQuery.holdReady,
|
||||||
added due to this ticket
|
see
|
||||||
<a href="http://bugs.jquery.com/ticket/6781">#6781</a>.
|
<a href="http://bugs.jquery.com/ticket/6781">#6781</a>
|
||||||
|
and
|
||||||
|
<a href="http://bugs.jquery.com/ticket/8803">#8803</a>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Test for jQuery.readyWait, which can be used
|
Test for jQuery.holdReady, which can be used
|
||||||
by plugins and other scripts to indicate something
|
by plugins and other scripts to indicate something
|
||||||
important to the page is still loading and needs
|
important to the page is still loading and needs
|
||||||
to block the DOM ready callbacks that are registered
|
to block the DOM ready callbacks that are registered
|
||||||
|
@ -68,7 +70,7 @@
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Script loaders are the most likely kind of script
|
Script loaders are the most likely kind of script
|
||||||
to use jQuery.readyWait, but it could be used by
|
to use jQuery.holdReady, but it could be used by
|
||||||
other things like a script that loads a CSS file
|
other things like a script that loads a CSS file
|
||||||
and wants to pause the DOM ready callbacks.
|
and wants to pause the DOM ready callbacks.
|
||||||
</p>
|
</p>
|
||||||
|
|
Loading…
Reference in a new issue