0

此代码适用于 Firefox 和 Chrome,但不适用于 IE9。它甚至可以在 IE9 中的同一个域上工作,但在其他域上却失败了。控制台向我显示 SCRIPT1002:语法错误。我在 jsp 中有此代码,并使用我的控制器中的 {domain}/path 将其加载到脚本标记中。

   (
        function(){
           var v = "1.9.1";
           if (window.jQuery === undefined || window.jQuery.fn.jquery < v ) {
           var done = false;
         var script = document.createElement("script");
         script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
         script.onload = script.onreadystatechange = function(){
        if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
         done = true;
            initBookmarklet();
        }
        };      
        document.getElementsByTagName("head")[0].appendChild(script);   
            } else {
                initBookmarklet();
            } 


            function initBookmarklet(){
                //do stuff here
            }
        }());
4

1 回答 1

0

试着这样写

var v = "1.9.1";
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
    var done = false;
    var script = document.createElement("script");
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
    script.onload = script.onreadystatechange = function () {
        if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
            done = true;
            initBookmarklet();
        }
    };
    document.getElementsByTagName("head")[0].appendChild(script);
} else {
    initBookmarklet();
}


function initBookmarklet() {
    //do stuff here
}
于 2013-08-01T13:08:48.580 回答