Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Seems very similar to this question: <a href="https://stackoverflow.com/questions/885990/linking-javascript-libraries-in-user-controls/886184#886184">Linking JavaScript Libraries in User Controls</a></p> <p>I'll repost my answer that that question here.</p> <p>I would definitely advise against putting them inside partials for exactly the reason you mention. There is a high chance that one view could pull in two partials that both have references to the same js file. You've also got the performance hit of loading js before loading the rest of the html.</p> <p>I don't know about best practice but I choose to include any common js files inside the masterpage and then define a separate ContentPlaceHolder for some additional js files that are specific to a particular or small number of views.</p> <p>Here's an example master page - it's pretty self explanatory.</p> <pre><code>&lt;%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %&gt; &lt;head runat="server"&gt; ... BLAH ... &lt;asp:ContentPlaceHolder ID="AdditionalHead" runat="server" /&gt; ... BLAH ... &lt;%= Html.CSSBlock("/styles/site.css") %&gt; &lt;%= Html.CSSBlock("/styles/ie6.css", 6) %&gt; &lt;%= Html.CSSBlock("/styles/ie7.css", 7) %&gt; &lt;asp:ContentPlaceHolder ID="AdditionalCSS" runat="server" /&gt; &lt;/head&gt; &lt;body&gt; ... BLAH ... &lt;%= Html.JSBlock("/scripts/jquery-1.3.2.js", "/scripts/jquery-1.3.2.min.js") %&gt; &lt;%= Html.JSBlock("/scripts/global.js", "/scripts/global.min.js") %&gt; &lt;asp:ContentPlaceHolder ID="AdditionalJS" runat="server" /&gt; &lt;/body&gt; </code></pre> <p>Html.CSSBlock &amp; Html.JSBlock are obviously my own extensions but again, they are self explanatory in what they do.</p> <p>Then in say a SignUp.aspx view I would have</p> <pre><code>&lt;asp:Content ID="signUpContent" ContentPlaceHolderID="AdditionalJS" runat="server"&gt; &lt;%= Html.JSBlock("/scripts/pages/account.signup.js", "/scripts/pages/account.signup.min.js") %&gt; &lt;/asp:Content&gt; </code></pre> <p>HTHs, Charles</p> <p>Ps. Here is a follow up question I asked about minifying and concatenating js files: <a href="https://stackoverflow.com/questions/890561/concatenate-minify-js-on-the-fly-or-at-build-time-asp-net-mvc">Concatenate &amp; Minify JS on the fly OR at build time - ASP.NET MVC</a></p> <p><strong>EDIT:</strong> As requested on my other answer, my implementation of .JSBlock(a, b) as requested</p> <pre><code>public static MvcHtmlString JSBlock(this HtmlHelper html, string fileName) { return html.JSBlock(fileName, string.Empty); } public static MvcHtmlString JSBlock(this HtmlHelper html, string fileName, string releaseFileName) { if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName"); string jsTag = string.Format("&lt;script type=\"text/javascript\" src=\"{0}\"&gt;&lt;/script&gt;", html.MEDebugReleaseString(fileName, releaseFileName)); return MvcHtmlString.Create(jsTag); } </code></pre> <p>And then where the magic happens...</p> <pre><code> public static MvcHtmlString MEDebugReleaseString(this HtmlHelper html, string debugString, string releaseString) { string toReturn = debugString; #if DEBUG #else if (!string.IsNullOrEmpty(releaseString)) toReturn = releaseString; #endif return MvcHtmlString.Create(toReturn); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload