简体   繁体   中英

Loading all JavaScripts in footer

I'm trying to load all JavaScripts in the footer of my page which includes scripts that are only required on a specific page, which means such scripts cannot be included in _Layout.cshtml .

If my view looks something like this:

@model Some.Model
<p>Some paragraphs....</p>
<script> include js here </script>

In my view source, I want to have

<html>
 .....
 .....

  <footer>
    <script>
      script from _Layout
      script only required by specific page
    </script>
  </footer>
</html>

Is this possible in ASP.NET MVC ?

If you are using the Razor view engine you could include a scripts section in your _Layout.cshtml:

    ...
    <footer>
        @RenderSection("scripts", required: false)
    </footer>
</body>

and then inside the specific view override this section and include only the script that is needed by this view:

@model Some.Model
<p>Some paragraphs....</p>

@section scripts {
    <script src="script only required by specific page"></script>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM