Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put scripts in MVC _layout page

Tags:

asp.net-mvc

Hello I understand from reading advice on this site that scripts should go at the bottom of the _layout page. My problem is that I am not sure exactly where the 'bottom' is. Some people have said it is just before the tag but that does not work for me. I have tried putting the script in many places but nowhere seems to work. Could someone please tell me what I am doing wrong.

This is my About page

@{
    ViewBag.Title = "About Us";
}

<h2>About</h2>
<p>
     Put content here.
</p>

<script type="text/javascript">
  $(function () {
    alert("Hello World");
  });
</script>

This is my _Layout page

<!DOCTYPE html>
<html>
<head>
  <title>@ViewBag.Title</title>
  <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
  @* jQuery works if this line is here but not if it is at the bottom
   <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
  *@
</head>
<body>
  <div class="page">
    <div id="header">
      <div id="title">
        <h1>
          My MVC Application</h1>
      </div>
      <div id="logindisplay">
        @Html.Partial("_LogOnPartial")
      </div>
      <div id="menucontainer">
        <ul id="menu">
          <li>@Html.ActionLink("Home", "Index", "Home")</li>
          <li>@Html.ActionLink("About", "About", "Home")</li>
        </ul>
      </div>
    </div>
    <div id="main">
      @RenderBody()
    </div>
    <div id="footer">
    </div>
  </div>
  @* jQery does not work if the script is here! *@
  <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
</body>
</html>
like image 536
Pete Davies Avatar asked Jul 19 '11 08:07

Pete Davies


People also ask

Where do you put Javascript on a razor page?

A JS file for the Index component is placed in the Pages folder ( Pages/Index. razor. js ) next to the Index component ( Pages/Index. razor ).

How add Javascript to MVC?

To include a JavaScript function, it is as simple as include a <script> tag and define the function inside the script block. Now, to call the function, add an onclick event on the employee name hyperlink and call the function.

What are the elements included in the _layout Cshtml?

_Layout.cshtml The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer. Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app.


2 Answers

Some Alternatives

It is absolutely possible to have your jquery at the end of your body tag. Ideally the only js you have in your head is modernizr, html5shiv or the like.

Option 1

Put a @RenderSection("scripts", required: false) after the jQuery <script... element as the last line inside your body element. Then in any of your views you would use

@section scripts
{
  <script type="text/javascript">
    $(function () {
      // page specific js code here
    });
  </script>
}

Option 2

Between your jQuery script element and the closing of your body element have a script element pointing to the relevant js file for the action/view in question:

<script 
  src="@Url.Content("~/Scripts/" + ViewContext.RouteData.GetRequiredString("action") + ".js")" 
  type="text/javascript">
</script>
like image 191
Scotty.NET Avatar answered Oct 21 '22 17:10

Scotty.NET


Jquery file has to be included before first reference to jquery is found on the document. So, to solve this problem, i put jquery file in head section and all other script that are not used in views but in other script files are included before closing body tag

like image 1
Muhammad Adeel Zahid Avatar answered Oct 21 '22 19:10

Muhammad Adeel Zahid