1

我正在构建一个依赖于加载 jquery 的小部件 - 我正在使用以下内容来加载它和我的代码:

(function () {

    var jqueryVersion = (window.jQuery !== undefined) ? window.jQuery.fn.jquery.charAt(0) + window.jQuery.fn.jquery.charAt(2) : 0;

    var jQuery;
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }

    /******** Load jQuery if not present *********/
    if (parseInt(jqueryVersion) < 17) {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
        script_tag.onload = scriptLoadHandler;
        script_tag.onreadystatechange = function () { // Same thing but for IE
            if (this.readyState === 'complete' || this.readyState === 'loaded') { scriptLoadHandler(); }
        };
        document.getElementsByTagName("head")[0].appendChild(script_tag);
    } else {
        jQuery = window.jQuery;
        main();
    }

    function main() {
        //my code goes in here
        //leaving blank for now because I still get error
    }

})();

我从这里得到了大部分内容:http: //alexmarandon.com/articles/web_widget_jquery/

我需要 jquery 1.7 或更高版本,因为我使用 .on() 方法。当我在使用 1.7 之前的 jquery 的页面上运行它时,我有时会在 IE 中遇到错误,这使我相信与旧版本的 jquery 或页面上的其他一些 js 存在冲突。一些错误:

SCRIPT5007:无法获取未定义或空引用的属性“forceInt”

SCRIPT5007:预期对象

SCRIPT5007:无法获取未定义或空引用的属性“缓动”

SCRIPT438:对象不支持“on”属性或方法

如果我改变这些错误就会消失

jQuery = window.jQuery.noConflict(true);

jQuery = window.jQuery.noConflict();

难道我做错了什么?

4

1 回答 1

3

Your problem might be caused by the fact that the 'handler' is called twice.

http://msdn.microsoft.com/en-us/library/ie/hh180173(v=vs.85).aspx

Also, taken from http://api.jquery.com/jQuery.noConflict/

"If necessary, you can free up the jQuery name as well by passing true as an argument to the method. This is rarely necessary, and if you must do this (for example, if you need to use multiple versions of the jQuery library on the same page), you need to consider that most plug-ins rely on the presence of the jQuery variable and may not operate correctly in this situation."

于 2013-04-19T20:18:24.733 回答