title: Proper place to load jQuery in MVC Layout view
body: <p>I've created an MVC 4 using the default template, where <code>@Script.Render(~"bundles/jquery")</code> is called after <code>@RenderBody()</code>.  As per <a href="https://stackoverflow.com/questions/11926405/jquery-after-body-asp-net-mvc-4">this</a> post, this is the recommended execution order to prevent the loading of scripts to block the rendering of the page.</p>
  
  <p>I want to add a small jQuery call in my view, or my <code>RenderBody()</code> section.  It looks like this, at the top of my view:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function()
  {
      $("#eta_table").tablesorter({
          headers: {
              0: { sorter: false }
          }
      });
  });
  </code></pre>
  
  <p></p>
  
  <p>However, this will always throw the error <code>Error: '$' is undefined</code> because jQuery isn't loaded until after the <code>RenderBody()</code>.  </p>
  
  <p>I can't imagine this is a new problem, it seems like a fairly common task...  Any suggestions on how this should be handled?</p>
  
  <p>For reference, here is where the jQuery is loaded:</p>
  
  <pre><code>        @Scripts.Render("~/bundles/jquery")
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Edit</strong></p>
  
  <p>I ended up moving the script above into my scripts.js file, and loaded it in below jQuery in the layout page, like so:</p>
  
  <pre><code>            bundles.Add(new ScriptBundle("~/bundles/custom").Include(
                      "~/Scripts/Custom/tablesorter.js",
                      "~/Scripts/Custom/scripts.js"));
  </code></pre>
  
  <p>And the HTML: </p>
  
  <pre><code>        @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/custom")
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Except this still seems wrong, as now the scripts are going to have to load for every page that uses the master layout view.  It's working, but, is this the best way?</p>