What does this pseudo-jquery function(r) script do?

javascript, jquery

Solution

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

Problem

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.

Original source