Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I want to be able to call external js file in mvc 5 _layout page, i have done a few researches on google, but no success, below is my code in the _Layout Head section:

JavaScript
<!-- start: MAIN JAVASCRIPTS -->
        <!--[if lt IE 9]>
            <script src="~/assets/plugins/respond.min.js"></script>
            <script src="~/assets/plugins/excanvas.min.js"></script>
            <script type="text/javascript" src="~/assets/plugins/jQuery-lib/1.10.2/jquery.min.js"></script>
        <![endif]-->
        <!--[if gte IE 9]><!-->
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/jQuery-lib/2.0.3/jquery.min.js")"></script>
        <!--<![endif]-->
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-ui/jquery-ui-1.10.2.custom.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/bootstrap/js/bootstrap.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/blockUI/jquery.blockUI.js")"></script>
        <script type="text/javascript" src="@Url.Content("~/assets/plugins/iCheck/jquery.icheck.min.js")"></script>
        
        <!-- end: MAIN JAVASCRIPTS -->

        <script>
            jQuery(document).ready(function () {
                Main.init();
                Index.init();
            });
        </script>

        @RenderSection("JavaScript", required : false)
        @RenderSection("CSS", required : false)


bottom of the _LayoutPage i have this:
JavaScript
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/assets/plugins")
@RenderSection("Scripts", required : false)


Then in my View page i have this:

JavaScript
@section JavaScript
{

    <!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.pie.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/flot/jquery.flot.resize.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery.sparkline/jquery.sparkline.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-easy-pie-chart/jquery.easy-pie-chart.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-ui-touch-punch/jquery.ui.touch-punch.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/plugins/fullcalendar/fullcalendar/fullcalendar.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/assets/js/index.js")">
        
    </script>
    <!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
}



i will appreciate if anyone could correct what i did wrong, thanks.
Posted
Comments
Krunal Rohit 26-Oct-15 4:52am    
If you're view is referenced to Layout page, then you'd be able to access the JS files.

-KR
Uwakpeter 26-Oct-15 5:02am    
yes, but i wonder why it isn't working.

1 solution

You are trying to use jQuery before it is defined. Move your js code to below where you include your script files, or put that code in a js file rather than on the layout and include it after you include jquery etc. You'll have the same issue with the scripts in your JavaScript section, they are also being included before jquery is. Your scripts are loaded and processed in order so where they appear on the page is important.
 
Share this answer
 
Comments
Uwakpeter 26-Oct-15 5:26am    
please can you give any example?
F-ES Sitecore 26-Oct-15 5:32am    
Example of what? You just need to re-order where your includes happen such that something isn't included before the thing it is referencing. So jquery plug-ins can't appear before jquery itself etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900