在MVC4脚本捆绑包中设置脚本类型 [英] Set Script Type in MVC4 Script Bundle
问题描述
我正在MVC4中使用新的ScriptBundle
类,以及@Styles.Render()
和@Scripts.Render()
.
I'm using the new ScriptBundle
class in MVC4, along with @Styles.Render()
and @Scripts.Render()
.
到目前为止,这对我一直很有效,直到我在Firefox中遇到此问题:
This has worked great for me so far, until I ran into this issue in Firefox :
If tags have a "type" attribute, it should equal "text/javascript" or "application/javascript". Also scripts must be parsable (syntactically correct).
果然,当我在使用.Render()
助手的任何页面上浏览时,我所有的JavaScript都输出为:
Sure enough, when I looked on any page where I'm using the .Render()
helper, all of my javascripts are being outputted as :
<script src="/WestingProject/Scripts/jquery.js">
<script src="/WestingProject/Scripts/jquery-1.9.1.js">
<script src="/WestingProject/Scripts/jquery.unobtrusive-ajax.js">
<script src="/WestingProject/Scripts/jquery.validate.js">
<script src="/WestingProject/Scripts/jquery.validate.unobtrusive.js">
<script src="/WestingProject/Scripts/jquery.carouFredSel-6.2.1.js">
<script src="/WestingProject/Scripts/jquery.dropdown.js">
<script src="/WestingProject/Scripts/jquery.placeholder.js">
这是我在BundleConfig.cs
类中定义它们的方式:
Here is how I have them defined in the BundleConfig.cs
class :
bundles.Add(
new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery.js",
"~/Scripts/jquery-1.9.1.js",
"~/Scripts/jquery.unobtrusive-ajax.js",
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.validate.unobtrusive.js",
"~/Scripts/jquery.carouFredSel-6.2.1.js",
"~/Scripts/jquery.dropdown.js",
"~/Scripts/jquery.placeholder.js"));
我的问题:
使用脚本包,有什么方法可以将type="text/javascript"
属性添加到我所有的javascript输出到页面上时?
Using the script bundle, is there any way I can add the type="text/javascript"
attribute to all of my javascripts when they're outputted to the page?
推荐答案
您可以执行以下操作:
<script src="@Scripts.Url("~/bundles/jquery")" type="text/javascript"></script>
尽管如此,这将最小化并捆绑您的脚本,因此将在需要时禁止调试javascript.
Although, this will minify and bundle your scripts, so will inhibit debugging the javascript if needed.
这篇关于在MVC4脚本捆绑包中设置脚本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!