Question

I have the following piece of code that emits all my javascript:

@using SquishIt.Framework.Minifiers.JavaScript
@{
    string[] scriptLinks = new[]
    {
        Url.Content("~/content/script/json2.js"),
        Url.Content("~/content/script/b.extensions.js"),
        Url.Content("~/content/script/jquery.extensions.js"),
        Url.Content("~/content/script/jquery.validate.min.js"),
        Url.Content("~/content/script/jquery.validate.unobtrusive.min.js"),
        Url.Content("~/content/script/jquery.validate.tooltip.js"),
        Url.Content("~/content/script/jquery.form.js"),
        Url.Content("~/content/script/jquery.placeholder.js"),
        Url.Content("~/content/script/jquery.tooltip.js"),
        Url.Content("~/content/script/jquery.signalr-0.5.2.js"),
        // TODO: Url.Content("~/signalr/hubs"), // SignalR auto-generated hubs proxy ? Get Programatically??
        Url.Content("~/content/script/b.core.js"),
        Url.Content("~/content/script/b.facebook.js"),
        Url.Content("~/content/script/b.twitter.js"),
        Url.Content("~/content/script/b.realtime.js"),
        Url.Content("~/content/script/b.init.js"),
        Url.Content("~/content/script/views/user/login.js"),
        Url.Content("~/content/script/views/post/create.js"),
        Url.Content("~/content/script/views/post/list.js")
    };
}
@{
@* we don't bundle external javascript links nor localization messages. *@
    const string jQueryVersion = "1.7.2";
    string jQueryExternal = "//ajax.googleapis.com/ajax/libs/jquery/{0}/jquery.min.js".FormatWith(jQueryVersion);
    string jQueryLocal = @Url.Content("~/content/script/jquery-{0}.min.js".FormatWith(jQueryVersion));
}
<script src="@Url.Action("Localization", "Resource")"></script>
<script src="@jQueryExternal"></script>
<script>!window.jQuery && document.write(unescape("%3Cscript src='@jQueryLocal'%3C/script%3E"))</script>
@(Bundle.JavaScript()
    .Add(scriptLinks)
    .WithoutTypeAttribute()
    .WithMinifier<YuiMinifier>()
    .WithConfiguration()
    .MvcRender("~/all.js?_=1")
)
@JavaScript.Emit()
@* TODO: analytics service (pixel goes to noscript partial) *@

This always works just fine except the scenario where I remove the external jQuery reference to the google cache: <script src="@jQueryExternal"></script> AND the rest of the scripts are minfied in a single script tag.

I also figured out that if I added any script tag after the document.write tag, such as this:

<script>!window.jQuery && document.write(unescape("%3Cscript src='@jQueryLocal'%3C/script%3E"))</script>
<script></script> @* work around an issue where javascript minified into a single script tag won't find references to jQuery if loaded in the previous script tag. *@

It does work.

Otherwise I get a "anything is undefined"

Tested in both chrome 20 and firefox 11, so what's that extra, empty script tag, doing exactly? and why does it mitigate this corner-case error?

Was it helpful?

Solution

turns out the issue was in the document.write element being improperly closed.

unescape("%3Cscript src='@jQueryLocal'%3C/script%3E")

into

unescape("%3Cscript src='@jQueryLocal'%3E%3C/script%3E")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top