由于加载jQuery时执行/不执行脚本 - 为什么? [英] A script is executed/not executed due to when jQuery is loaded - Why?

查看:67
本文介绍了由于加载jQuery时执行/不执行脚本 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到的问题。试试这个代码

This is a problem I have. Try this code:

if (typeof jQuery == 'undefined') {
    function getScript(url, success) {
        var script = document.createElement('script');
        script.src = url;

        var head = document.getElementsByTagName('head')[0];

        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;
                head.removeChild(script);
            };
        };

        head.appendChild(script);
    };

    getScript('http://code.jquery.com/jquery-1.11.2.min.js', function () {
        if (typeof jQuery !== 'undefined') {
            jQuery(document).ready(function ($) {
                MyFunction($);
            });
        }
    });   
} else {
    jQuery(document).ready(function ($) {
        MyFunction($);
    });
}

function MyFunction($) {
    $.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", function (d) {
    }).done(function(d) { 
        JsonToHtml(d);
    });
}    

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

    $('body').prepend(items.join(''));
} 

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

you will notice that my code check if jQuery is loaded. If not, it loads a version from external source; than retrieve a JSON, parse it and "execute it".

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

As you can see, the script loaded inside the body it is not loaded at all (this is my problem).

现在,尝试在小提琴中选择jQuery的版本/库(1.8.3没问题)并按下播放:你会看到脚本/按钮渲染:脚本是执行!!!

Now, try to choose a version/library of jQuery in the fiddle (1.8.3 is ok) and press play: you will see the script/button render as well: the script is executed!!!

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

Why loading jQuery first (here) render the script, and load jQuery later won't execute the script? Can you help me?

推荐答案

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

I think your best bet is to force onload event to be refired if it is already fired because as you are loading jQuery (if undefined), this event is already fired. This is a workaround:

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 -

这篇关于由于加载jQuery时执行/不执行脚本 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆