Why Asp.Net MVC 5 put @Scripts.Render(“~/bundles/jquery”) at the bottom in _Layout.cshtml?

前端 未结 4 1776
臣服心动
臣服心动 2021-02-04 02:18

I put

相关标签:
4条回答
  • 2021-02-04 02:18

    Just enclose it inside section scripts in .cshtml Page as shown.

    @section scripts{
      <script>
      $(document).ready(function () { 
        // .....
      });
      </script>
    }
    
    0 讨论(0)
  • 2021-02-04 02:32

    You can use sections :

    in your layout :

    ...
    <script src="/Scripts/jquery-1.10.2.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    @RenderSection("scripts", required: false)
    ...
    

    in yours cshtml :

    @section scripts {
        <script>
            $(document).ready(function () { /// $ not defined.
                // .....
            });
        </script>
    }
    
    0 讨论(0)
  • 2021-02-04 02:33

    It's a best practice to load your javascript files after every possible HTML element.

    Knowing this, I would place your document ready handler after loading all libs.

     <hr />
        <footer>
        </footer>
    </div>
    
    <script src="/Scripts/jquery-1.10.2.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    <script>
        $(document).ready(function () { /// $ not defined.
            // .....
        });
    </script>
    
    0 讨论(0)
  • 2021-02-04 02:36

    If the script is at the top of the page, and there are problems then it may cause the page to stop/take a long time loading. Putting them at the bottom allows the page the render fully before your scripting goes to work.

    0 讨论(0)
提交回复
热议问题