where should i place the js script files in a mvc application so jquery works well?

后端 未结 1 1518
醉梦人生
醉梦人生 2020-12-01 22:34

the _layout.cshtml has code lines

@Scripts.Render(\"~/bundles/jquery\")    
@Styles.Render(\"~/Content/css\")
@Scripts.Render(\"~/bundles/modernizr\") 


        
相关标签:
1条回答
  • 2020-12-01 23:23

    In your layout file, the script tag to load jQuery library is included at the end of the page. But there is another section called scripts below that. So in your individual pages ( Ex : Index,View etc..) You should be putting your javascript inside the section scripts

    <body>
     @RenderBody()
    
     @Scripts.Render("~/bundles/jquery")  
     @RenderSection("scripts", required: false)
    </body>
    

    And in your views (Ex : Index view)

    @section scripts{
    
        <script src="~/Scripts/SomePageSpecificFile.js"></script>
        <script>
         $(function(){
            // Your other js code goes here
         });
       </script>
    
    }
    

    So when razor render the page it will be

    <body>
        <script src="/Scripts/jquery-1.10.2.js"></script>
        <script src="~/Scripts/SomePageSpecificFile.js"></script>
        <script>
          $(function(){
            // Your other js code goes here
          }); 
       </script>
    </body>
    

    As long as you execute your page specific javascript which uses jQuery inside the scripts section ,you should be fine.

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