I have the following
bundles.Add(new ScriptBundle("~/bundles/scripts/common").Include(
"~/Scripts/jquery.validationEngine.js",
"~/Scripts/common.js"));
Which generates
<script src="/bundles/scripts/common?v=9O0Yi3fV_GWpGyJQ_QYURiOYy6SEmxUQtkUVN4GXo2U1"></script>
When rendered with
<asp:PlaceHolder ID="PlaceHolderJs" runat="server">
<%: Scripts.Render("~/bundles/scripts/common") %>
</asp:PlaceHolder>
Which is not valid HTML as its missing type="text/javascript". How do I make the BundleCollection output this element in the script tag?
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute.
JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
One way is to change how you render your scripts:
From:
@Scripts.Render("~/bundles/scripts/common")
To:
<script src="@BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")" type="text/javascript"></script>
Or depending on how you are implementing bundling, you may need:
<script src="@Microsoft.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")" type="text/javascript"></script>
Or for web forms:
<script src="<%= Microsoft.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")%>" type="text/javascript"></script>
Although if there is a way to do it using @Script.Render I'd like to see it.
UPDATE: in response to your comments, as specified in this SO answer, in the pre-release version of System.Web.Optimization
, there is an option called RenderFormat
that will let you do this as well... but I think the stuff above is easier to read for this particular case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With