We have this in the HEAD of our site, and we're not sure what it does. It might have been a debug thing left over from a consulting firm? Thanks for any clues.
<script>
(function (r) {
_ready = {
q: function () {
return r;
}
};
$ = function (f) {
if (typeof f === "function") {
r.push(arguments);
}
return $;
};
jQuery = $.ready = $;
}([]));
</script>
FYI, the reason we're looking at this critically is that it seems to conflict with some other jquery libraries, such as NRelate's.
This will help for sure
From the link given above,
It setups jQuery.ready Callbacks Before jQuery is Loaded
e.g.
Suppose you have jQuery in body like this,
<div id="main">
<script>
$(function(){
$("#main").prepend( "<p>Heyo!</p>" );
});
</script>
</div>
<div>...more HTML...</div>
<script src="/js/jquery.js"></script>
It wont work, since jQuery is being loaded at bottom and you are trying to use it before that.
So, we do this workaround,
<head>
<script>
(function(a){
_ready = {
q: function () {
return r;
}
};
$ = function (f) {
if (typeof f === "function") {
r.push(arguments);
}
return $;
};
jQuery=$.ready=$;
}([]));
</script>
</head>
<body>
<div id="main">
<script>
$(function() {
$( "#main" ).prepend( "<p>Heyo!</p>" );
});
</script>
<div>...more HTML...</div>
</div>
<script src="/js/jquery.js"></script>
<script>
(function( i, s, q, l ) {
for( q = window._ready.q(), l = q.length; i < l; ) {
$.apply( this, s.call( q[ i++ ] ) );
}
window._ready.q = undefined;
}( 0, Array.prototype.slice ));
</script>
<script src="/js/scripts.js"></script>
</body>
What the first script does is emulate jQuery's ready function by storing the argments of any calls to $.ready where the first argument is a function into an array. This array is private to our globally scoped _ready.q method, which, when called, returns the array.
The second script loops through the array by calling _ready.q() and then applys the arguments originally passed to our imposter $.ready to the real $.ready.
P.S. its a self invoking function in which an empty array is passed with variable name r.
Refer this
r will be the empty array passed into the function at the end.
Fully annotated copy:
// v--- receives the [] passed in below
(function (r) {
// Creates an implicit global variable called `_ready`
// (or overwrites one that's already there)
_ready = {
// Makes `r` available via `_ready.q()` or
// `window._ready.q()`
q: function () {
return r;
}
};
// Creates an implicit global variable called `$`
// (or overwrites one that's already there)
$ = function (f) {
if (typeof f === "function") {
r.push(arguments);
}
return $;
};
// Creates/overwrites the global variable `jQuery` and `$.ready`
// with the function defined above
jQuery = $.ready = $;
}([]));
//^^-- the value passed to r
It would seem to allow you to create an array of functions to call when jQuery is actually loaded, based on your taking over the jQuery, $, and $.ready symbols. The code to actually do that is not shown. The array will be accessible to whatever code actually uses it via _ready.q() (or window._ready.q()).
And to call it convoluted and in need of serious commenting (not to mention declaring the implicit global — shudder —) would be an understatement.
See also:
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