Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery getScript method's internal process

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') ?

like image 357
Freshblood Avatar asked Jul 10 '11 15:07

Freshblood


1 Answers

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 );
}
like image 78
Felix Kling Avatar answered Sep 30 '22 03:09

Felix Kling