简体   繁体   English

Head.js怪异的动作

[英]Head.js weird action

I'm trying to use head.js. 我正在尝试使用head.js。 Getting some weird action. 得到一些奇怪的动作。 Below works but if I comment out "alert('hi')" in the script in the BODY, then the head.js doesn't seem to work. 下面的方法有效,但是如果我在BODY脚本中将“ alert('hi')”注释掉,那么head.js似乎不起作用。

So I tried commenting out all the head.js stuff and used a standard tag for each JS library. 因此,我尝试注释掉所有head.js内容,并为每个JS库使用了一个标准标记。 And it works as expected. 它按预期工作。 But when using Head.js, it fails if the "alert('hi')" is commented out. 但是在使用Head.js时,如果注释掉“ alert('hi')”,它将失败。 Obviously, for production, I want to remove this alert but then I get this error message: "ReferenceError: $ is not defined" ... ??? 显然,对于生产环境,我想删除此警报,但随后收到此错误消息:“ ReferenceError:未定义$” ... ??? But if I uncomment out the alert, then it all works and pulls data from the DB ??? 但是,如果我取消对警报的注释,那么它将全部起作用并从数据库中提取数据?

Any thoughts? 有什么想法吗?

<head>
<title></title>

 <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js">           
</script>

<script type="text/javascript">
    head.js(
        /* root level */
        "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", 
        "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
        "/assets/scripts/external/_references.js",
        "../../assets/scripts/external/jquery.unobtrusive-ajax.min.js",
        "../../assets/scripts/external/jquery.validate-vsdoc.js",
        "../../assets/scripts/external/jquery.validate.min.js",
        "../../assets/scripts/external/jquery.validate.unobtrusive.min.js",
        "../../assets/scripts/external/knockout-2.3.0.js",
        "../../assets/scripts/external/modernizr-2.6.2.js",

        "/assets/css/Index.css",           
        "assets/css/OrangeCounty.css"                       
    );
</script>
</head>

<body>    
<div id="list"></div>

<script>
    alert('hi');
    $.getJSON("http://localhost/webapi/api/ccc", function (result) {
        $.each(result, function (i, field) {
            $("div").append(field.Code + " " + field.Country1 + "<br/>");
        });
    });
</script>              
</body>

The alert() stalls the script execution enough for jQuery to actually load. alert()使脚本执行停滞不前,足以使jQuery实际加载。 Head.js loads the scripts in parallel and doesn't block, so you have to use it with a callback: Head.js并行加载脚本并且不会阻塞,因此您必须将其与回调一起使用:

head.js(..., function() {
    // Your code
});

Or put that code into a separate script file and put it after jQuery in your head.js call. 或者将该代码放入一个单独的脚本文件中,并将其放在您的head.js调用中的jQuery之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM