Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for the detailed explanation. Last night I was thinking about overriding the base page and adding a Scripts/Css collection property which would then be filled by some htmlhelpers. The page would then spit them out in a contentplaceholder just like yours. Would this work or would you advise against it?
      singulars
    2. COSorry, I don't follow what you mean. "Overriding the base page" - Do you mean overriding System.Web.Mvc.Controller or System.Web.Mvc.ViewPage or simply creating your own custom viewdata model? "filled by some htmlhelpers" That throws me too... if you're using custom HtmlHelpers (from inside a view) then just use the ContentPlaceHolder method mentioned above. Without more info I would advise you to simply follow the above example as it is... even with more info I'd probably still advise you to do the above. ;-D
      singulars
    3. COI'll see if I can make a working example by the end of the day and post it here as an answer, if it works, I'd like opinions. I would override the ViewPage and add a property called Scripts which is a collection. Inside a partial view you would then call an htmlhelper to add a script Html.JSBlock("path/to/script.js" new{some options}). This helper adds the script to the Scripts collection of the parent page. In the parent page's view, inside the headplaceholder, it calls another htmlhelper which renders the script tags from the Scripts property. Or is that bad design?
      singulars
 

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