我有以下代码发出我的所有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) *@
除了我删除google缓存的外部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中都进行了测试,那么这个额外的空脚本标签到底做了什么?为什么它会缓解这个角落错误?
答案 0 :(得分:1)
结果证明问题是在document.write元素中未正确关闭。
unescape("%3Cscript src='@jQueryLocal'%3C/script%3E")
到
unescape("%3Cscript src='@jQueryLocal'%3E%3C/script%3E")