如何在 MVC 5 _Layout 页面中加载外部 Javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33341143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:22:42  来源:igfitidea点击:

How to load external Javascript file in MVC 5 _Layout Page

javascriptasp.net-mvc

提问by UwakPeter

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:

我希望能够在 mvc 5 _layout 页面中调用外部 js 文件,我在 google 上做了一些研究,但没有成功,下面是我在 _Layout Head 部分的代码:

<!-- 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)

Then at the bottom of the _Layout page i have this:

然后在 _Layout 页面的底部我有这个:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/assets/plugins")
@RenderSection("Scripts", required : false)

In the View, i have this:

在视图中,我有这个:

@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 -->
}

Please i will appreciate i someone could assist in suggesting what to do. thanks

请我将不胜感激我有人可以协助建议做什么。谢谢

采纳答案by Seabizkit

move

移动

@Scripts.Render("~/bundles/jquery")

to before its dependencies

在其依赖项之前

i.e.

IE

<script type="text/javascript" src="@Url.Content("~/assets/plugins/jquery-ui/jquery-ui-1.10.2.custom.min.js")"></script>

requires JQuery.js

需要 JQuery.js

but you load it after. So moving the include of JQuery above the script which required it, will fix that issue.

但你之后加载它。因此,将 JQuery 的包含移至需要它的脚本之上,将解决该问题。

Please apply this to all scripts and their dependencies

请将此应用于所有脚本及其依赖项

回答by Szel

Just like in normal HTML:

就像在普通 HTML 中一样:

<script type="text/javascript" src="http://link-to-external-javascript-file.js"></script>