我有以下一段代码可以发出我所有的 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) *@
除了我删除对谷歌缓存的外部 jQuery 引用的情况外,这总是可以正常工作<script src="@jQueryExternal"></script>
:并且其余脚本被压缩在单个脚本标记中。
我还发现如果我在 document.write 标记之后添加任何脚本标记,例如:
<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. *@
它确实有效。
否则我会得到一个“任何东西都是未定义的”
在 chrome 20 和 firefox 11 中进行了测试,那么那个额外的空脚本标签到底在做什么?为什么它可以减轻这种极端情况的错误?