未捕获的ReferenceError:虽然加载了jquery,但未定义$

时间:2015-05-07 13:58:28

标签: jquery

我在.aspx页面上有一些javascript但是看起来即使google chrome页面检查器正在加载jquery,也没有定义jquery。脚本通过bundle config加载。

捆绑配置

bundles.Add(new ScriptBundle("~/bundles/allscripts").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery-migrate-{version}.js",
            "~/Scripts/jquery-ui-{version}.js",
            "~/Scripts/Common.js",
            "~/Scripts/knockout-{version}.js",
            "~/Scripts/knockout.mapping-latest.js",
            "~/Scripts/knockout-ext.js",
            "~/Scripts/jquery.maskedinput.js",
            "~/Includes/Lightbox/lightbox.js",
            "~/Scripts/scrollTo.js",
            "~/Scripts/modernizr-{version}.js",
            "~/Scripts/Watermark.js",
            "~/Scripts/jquery.validate.js",
            "~/Scripts/json2.js",
            "~/Scripts/jquery.autosave.js",
            "~/Scripts/bootstrap.js",
            "~/Scripts/jquery.fileupload.js",
            "~/Scripts/toastr.js"
            ));

的JavaScript

<script type="text/javascript">
    $(function () {
        var displayMessage = false;
        var queryForEligibleUser = $.getJSON('../Mvc/Home/DetermineIfWeShouldShowMessage', function (data) {
            var result = $.parseJSON(data);
            displayMessage = result;
        }).done(function () {
            console.log("Display  message: ", displayMessage);
            if (displayMessage) {
                $.getJSON('../Mvc/Home/GetMessageToDisplay', function (messageToDisplay) {
                    console.log(messageToDisplay);
                    $('#DisplayMessage').text(messageToDisplay);
                });
            }
        });
    });
</script>   

在页面底部附近的脚本下方是脚本的加载位置。

<![if (gt IE 8) | (!IE)]>
<script src="/Scripts/jquery-2.1.3.js"></script>
<script src="/Scripts/jquery-migrate-1.2.1.js"></script>
<script src="/Scripts/jquery-ui-1.11.4.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/modernizr-2.8.3.js"></script>
<script src="/Scripts/Common.js"></script>
<script src="/Scripts/knockout-3.3.0.debug.js"></script>
<script src="/Scripts/knockout.mapping-latest.debug.js"></script>
<script src="/Scripts/knockout-ext.js"></script>
<script src="/Scripts/jquery.maskedinput.js"></script>
<script src="/Includes/Lightbox/lightbox.js"></script>
<script src="/Scripts/scrollTo.js"></script>
<script src="/Scripts/Watermark.js"></script>
<script src="/Scripts/json2.js"></script>
<script src="/Scripts/jquery.autosave.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/jquery.fileupload.js"></script>
<script src="/Scripts/toastr.js"></script>

1 个答案:

答案 0 :(得分:0)

我在这里找到了一个解决方案。enter link description here

我将脚本移动到一个单独的文件中,然后将引用放在页面底部。

<script type="text/javascript" defer="defer" src="../Areas/Mvc/Scripts/ShowMessage.js"></script>

我使用了defer,因此在呈现HTML后会下载脚本。