最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - how to call javascript on layout mvc - Stack Overflow

programmeradmin2浏览0评论

I try to run a basic javascript in all my pages (before load another html code), then I add my js function to _Layout.cshtml but doesn't work (I don't how is correct way to do it). Thanks in advance.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        <meta name="viewport" content="width=device-width" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        @Styles.Render("~/Content/mobileCss", "~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        <script type="text/javascript">
            $(document).ready(function () {
                alert("Do something");
            });
        </script>
    </head>
    <body>

        <div data-role="page" data-theme="b">
            <div data-role="content">
                @RenderBody()
            </div>
        </div>

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

I try to run a basic javascript in all my pages (before load another html code), then I add my js function to _Layout.cshtml but doesn't work (I don't how is correct way to do it). Thanks in advance.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        <meta name="viewport" content="width=device-width" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        @Styles.Render("~/Content/mobileCss", "~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        <script type="text/javascript">
            $(document).ready(function () {
                alert("Do something");
            });
        </script>
    </head>
    <body>

        <div data-role="page" data-theme="b">
            <div data-role="content">
                @RenderBody()
            </div>
        </div>

        @Scripts.Render("~/bundles/jquery", "~/bundles/jquerymobile")
        @RenderSection("scripts", required: false)
    </body>
</html>
Share Improve this question edited Nov 23, 2012 at 16:28 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Nov 23, 2012 at 11:50 sebagiarsebagiar 1551 gold badge2 silver badges11 bronze badges 1
  • FYI: if you place you javascript at the bottom of HTML, just before the closing </body> tag, it will be executed when DOM has been plete. Thus making the use of $.ready() pointless, obsolete and wasteful. – tereško Commented Nov 23, 2012 at 16:36
Add a ment  | 

1 Answer 1

Reset to default 5
@Scripts.Render("~/bundles/jquery", "~/bundles/jquerymobile")

must be before

    <script type="text/javascript">
        $(document).ready(function () { // JQuery not loaded here, $ is undefined
            alert("Do something");
        });
    </script>
发布评论

评论列表(0)

  1. 暂无评论