jQuery getScript method's internal process

妖精的绣舞 提交于 2019-12-23 12:34:54

问题


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 .

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

$.getScript('gallery.js') is exactly same thing with $('<script src="gallery.js">').appendTo('body') ?


回答1:


This is the interesting part in the source code.

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

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

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 );
}



回答2:


Luke, use the source.

(note these links are to an old commit)



来源:https://stackoverflow.com/questions/6642081/jquery-getscript-methods-internal-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!