由于加载jQuery时执行/不执行脚本 - 为什么?

时间:2015-05-05 15:09:59

标签: javascript jquery

这是我遇到的问题。试试这个code

-(IBAction)suffle:(id)sender
{
    NSMutableArray *arrayExample = [[NSMutableArray alloc] init];

    [arrayExample addObject:[NSNumber numberWithInt:1]];
    [arrayExample addObject:[NSNumber numberWithInt:2]];
    [arrayExample addObject:[NSNumber numberWithInt:3]];
    [arrayExample addObject:[NSNumber numberWithInt:4]];

    for (int i = 0; i < [arrayExample count]; ++i) {
        int r = (random() % [arrayExample count]);
        [arrayExample exchangeObjectAtIndex:i withObjectAtIndex:r];
        NSLog(@"%@", [arrayExample description]);
    }
}

您会注意到我的代码检查是否加载了jQuery。如果没有,它从外部源加载一个版本;而不是检索JSON,解析它并执行它&#34;执行它#34;。

正如您所看到的,在主体内部加载的脚本根本没有加载(这是我的问题)。

现在,尝试在小提琴中选择jQuery的版本/库(1.8.3就可以了)并按下播放:您将看到脚本/按钮呈现:脚本已执行!!!

为什么首先加载jQuery(这里)渲染脚本,然后加载jQuery以后不会执行脚本?你能救我吗?

2 个答案:

答案 0 :(得分:2)

我认为最好的办法是强制onload事件被激活,因为当你加载jQuery(如果未定义)时,这个事件已经被触发了。这是一种解决方法:

function JsonToHtml(html) {
    var items = [];
    $.each(html, function (key, val) {
        items.push(val);
    });

    $('body').prepend(items.join(''));
    if (document.readyState === 'complete') { // check if document is complete
        var evt = document.createEvent('Event');
        evt.initEvent('load', false, false);
        window.dispatchEvent(evt); // then redispatch onload event
    }
}

-DEMO-

答案 1 :(得分:-1)

我认为问题在于范围。函数MyFunction()和JsonToHtml()不在范围内。 (记住你正在处理像getJSON这样的异步函数)也许我的解释是错误的,但代码可以工作。 :P

使用此code,您没有任何问题。

function _test(){}
_test.prototype = {
    hasjQuery: function(){
        if(typeof window.jQuery !='undefined' && !_test.prototype.otherLibrary() ) {
            return true;
        }else{
            return false;
        }
    },
    otherLibrary: function(){
        if (typeof document.$ == 'function') {
            return true;
        }else{
            return false;
        }
    },
    inyectjQuery: function(url, success){
        var script = document.createElement('script');
        script.src = url;
        script.id = "delete";
        done = false;
        script.onload = script.onreadystatechange = function() {
            if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                done = true;
                success();
                script.onload = script.onreadystatechange = null
            }
        };
        document.getElementsByTagName('head')[0].appendChild(script)
    },
    myFunction: function(){
        urljQuery = 'http://code.jquery.com/jquery-latest.min.js';
        if(_test.prototype.hasjQuery()){
            jQuery.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", 
                function (d) {
                    _test.prototype.JsonToHtml(d);
                }).done(function() { 
                    console.log("Success getJSON action");
                });
        }else{
            _test.prototype.inyectjQuery(urljQuery, function(){
                if (typeof window.jQuery == 'undefined') {
                    console.log("unable to load jQuery");
                }else{
                    jQuery.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", 
                    function (d) {
                        _test.prototype.JsonToHtml(d);
                    }).done(function() { 
                        console.log("Success getJSON action");
                    });
                }
            });
        }
    },
    JsonToHtml: function(html){
        var items = [];
        jQuery.each(html, function (key, val) {
            items.push(val);
        });

        jQuery('body').prepend(items.join(''));
    }
}
test = new _test();
test.myFunction();