jQuery getScript方法的内部过程 [英] jQuery getScript method's internal process

查看:101
本文介绍了jQuery getScript方法的内部过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解getScript的内部过程。我知道它在内部使用$ .get方法。我认为jQuery将一个脚本标记引用放入DOM中,以便能够执行该js文件,但是我找不到DOM中getScript的加载脚本的脚本引用。

I am trying to understand the internal process of getScript. I am aware that it uses $.get method interally.I was thinking that jQuery puts a script tag reference into the DOM for being able execute that js file but I couldn't find script references of loaded scripts by getScript in the DOM .

那么jQuery如何在DOM中没有脚本标记引用的情况下执行加载的脚本?

So how does jQuery execute loaded scripts without script tag references in the DOM?

$。getScript('gallery.js') $('< script src =gallery.js>')完全相同。appendTo('身体')

推荐答案

这是源代码中有趣的部分

jQuery似乎只接收文本并对其进行评估全局范围:

jQuery seems to just receive the text and evaluates it in global scope:

converters: {
    "text script": function( text ) {
        jQuery.globalEval( text );
        return text;
    }
}

如果您从其他域加载脚本, jQuery添加了一个新的脚本标签:

In case you load the script from a different domain, jQuery adds a new script tag:

head.insertBefore( script, head.firstChild );

但在代码加载后将其删除:

but removes it once the code was loaded:

// Remove the script
if ( head && script.parentNode ) {
    head.removeChild( script );
}

这篇关于jQuery getScript方法的内部过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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