admin管理员组文章数量:1356566
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.
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.
Share Improve this question edited May 25, 2012 at 22:36 Dogweather asked May 25, 2012 at 21:16 DogweatherDogweather 16.9k18 gold badges65 silver badges83 bronze badges 5-
Hmmm it looks like it's wiring up
document.ready
? – Mathew Thompson Commented May 25, 2012 at 21:19 - 2 It might be a placeholder for jQuery. If for any reason jQuery not loads this way your pages will not generate errors. Does your jQuery loads from a CDN or somewhere outside your site? – Emre Erkan Commented May 25, 2012 at 21:22
- I agree, it's a jQuery placeholder. +1 – Ryan Lynch Commented May 25, 2012 at 21:28
- So it looks like it's supporting jquery code embedded in the middle of the HTML document. Which unfortunately means if we rip this out, some things may not work. – Dogweather Commented May 25, 2012 at 22:35
-
Yes, but I would remend (if its possible) to findout those embedded codes in your body and place them after loading
jQuery.js
. I dont think, its a good practise...its just a workaround. – Jashwant Commented May 26, 2012 at 5:43
4 Answers
Reset to default 7This 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 menting (not to mention declaring the implicit global — shudder —) would be an understatement.
See also:
- The Horror of Implicit Globals
It's creating a list of all of the jQuery functions called (and the supplied parameters), probably before jQuery is actually loaded.
I expect you'll find some other piece of code that reads the contents of _ready.q()
and passes them to jQuery once it's actually loaded.
r
is the empty array which is located at the bottom []
, basically r
is an alias of the array which will be used inside the anonymous function as such r.push
will push an item into the empty array
(function(){ ... })()
is defined as an anonymous function.
本文标签: javascriptWhat does this pseudojquery function(r) script doStack Overflow
版权声明:本文标题:javascript - What does this pseudo-jquery function(r) script do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962881a2569355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论