简体   繁体   English

IE8-使用javascript回调函数的jQuery加载问题

[英]IE8 - jQuery loading issue using javascript call back function

I'm loading jQuery dynamically using JavaScript. 我正在使用JavaScript动态加载jQuery。 For loading jQuery, callback function is defined and in its call back function call doing some jQuery stuff. 为了加载jQuery,定义了回调函数,并在其回调函数中调用了一些jQuery的东西。

Works great in Firefox, chrome and IE9 as expected but in IE8 gives error message like "$ is not defined" mean there is issue with call back function execution in IE8. 可以在Firefox,Chrome和IE9中按预期工作,但在IE8中会显示错误消息,例如“未定义$”,这表示IE8中的回调函数执行存在问题。 I have spent a whole day to find out solution but not getting any way. 我花了一整天的时间来找出解决方案,但没有任何办法。

<body>    
<script language="javascript" type="text/javascript">
    function loadjQuery(callback) {
        var ver = getInternetExplorerVersion();

        var body = document.getElementsByTagName('body')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        if (ver == 8.0) {
            script.onload = callback.call();
        }
        else {
            script.onload = callback;
        }
        script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
        body.appendChild(script);
    }
    loadjQuery(function () {
        alert($(window).height());       
    });

    function getInternetExplorerVersion() {
        var rv = -1;
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }
    </script>
</body>

I'm totally stuck. 我完全被困住了。 Any help would appreciable? 有什么帮助的吗?

I used to code this function as well and the following code seems working perfectly: 我也曾经编写过此函数的代码,以下代码似乎运行良好:

            // Attach handlers for all browsers
            script.onload = script.onreadystatechange = function() {

                if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                    done = true;                        
                    // callback function provided as param
                    if(success != null)
                    {
                        success();
                    }
                    script.onload = script.onreadystatechange = null;
                    //head.removeChild(script);                     
                };              
            };     

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

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