0

I am reviewing some code in our web site until I came to this one

<script>
    (function (a) {
        _q = function () { return a; };
        $ = function (f) { typeof f === 'function' && a.push(arguments); return $; };
        jQuery = $.ready = $;
    }([]));
</script>  

And then followed by bunch of plugins including jQuery.
I just don't get what it means, I want to know how it works so I would be able to manipulate the code. Can anyone explain the code line by line?

Edit

So sorry for the bad codes. I was also wandering of its use that's why I ask it here. Anyway this is the whole code inside the head tag.

<head>
    <meta charset="utf-8">
    @foreach (string domain in
    ViewBag.Prefetches is string[] ? ViewBag.Prefetches : new string[] { })
    {
        @Html.Prefetch(domain)    
    }
    <title>
        @{@ViewBag.SiteName
            if (ViewBag.Title != null)
            {  @ViewBag.SiteDelimiter @ViewBag.Title } 
        }
    </title>

    <meta name="description" content="">
    <meta name="author" content="">
    <meta http-equiv="imagetoolbar" content="false">

    <link href="@Url.ContentArea("~/css/global/normalize.css")" rel="stylesheet" />

    @foreach (string stylesheet in
    ViewBag.Stylesheets is string[] ? ViewBag.Stylesheets : new string[] { @"style.css" })
    { 
        <link href="@Url.ContentArea("~/css/" + stylesheet)" rel="stylesheet" />
    }

    @RenderSection("CustomCss", required: false)

    <link href="@Url.ContentArea("~/css/global/helpers.css")" rel="stylesheet" />
    <link href="@Url.ContentArea("~/css/global/media.css")" rel="stylesheet" />

    <script src="@Url.ContentArea("~/js/global/modernizr-2.5.3.min.js")"></script>

    <script>
        (function (a) {
            _q = function () { return a; };
            $ = function (f) { typeof f === 'function' && a.push(arguments); return $; };
            jQuery = $.ready = $;
        }([]));
    </script>

    @RenderSection("OverrideJs", required: false)
</head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="@Url.ContentArea("~/js/global/jquery-1.7.2.min.js")"><\/script>');</script>
@foreach (string script in
    ViewBag.Scripts is string[] ? ViewBag.Scripts : new string[] { })
{ 
    <script src="@script"></script>
}
<script>
    Modernizr.load({
        test: window.JSON,
        nope: '@Url.Content("~/Scripts/Polyfills/json.polyfill.js")'
    });

    (function (i, s, q, l) {
        for (q = window._q(), l = q.length; i < l;) {
            $.apply(this, s.call(q[i++]));
        }
    }(0, Array.prototype.slice));

</script>
<!--[if lt IE 7 ]>
  <script src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.2/CFInstall.min.js"></script>
  <script>window.attachEvent("onload",function(){CFInstall.check({mode:"overlay"})})</script>
<![endif]-->

I can't provide a link because these codes were used in specific pages where you have to be logged in.
Anyway thanks for the help.

2
  • Without looking at the whole thing it's a bit difficult to guess what this is actually doing. Can you provide an url ? Commented Oct 20, 2012 at 2:29
  • I can't provide link, because the codes were used in specific pages where you have to be logged in. Commented Oct 20, 2012 at 2:43

1 Answer 1

1

Short answer: It logs if jQuery's been loaded

Long answer: It looks like all it does is add a variable "jQuery" if it's loaded, after adding to it a function _q that returns an array of arguments (which is an array-like variable that exist in all functions' execution contexts, thanks Fabrício Matté). As far as I can tell since no arguments are sent into the function, all it'll do is log the scope it's been called from.

It looks like the "jQuery" variable is accessed a bit further down, and if not set it loads jQuery. But I don't think it could be accessed down there anyways since I don't see the variable globally declared anywhere.

What it's used for is that it first tries to load jQuery from Google's hosting. If that fails it loads jQuery locally.

Sign up to request clarification or add additional context in comments.

1 Comment

arguments is an array-like variable that exist in all functions' execution contexts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.