5

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

oivvio
3,1662 gold badges36 silver badges42 bronze badges
asked Jul 10, 2011 at 15:56
0

2 Answers 2

7

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 );
}
answered Jul 10, 2011 at 16:05
Sign up to request clarification or add additional context in comments.

1 Comment

Even jQuery remove added scripts. Try this $('<script src="sample.js">').appendTo('body');
5

Luke, use the source.

(note these links are to an old commit)

answered Jul 10, 2011 at 15:59

3 Comments

@ZMorek yes. This answer is 18 months old, and jQuery has continued active development. I've updated to link to a revision from back then.
@MattBall - can/would you update it again please? :)
You found the source, but I don't know that you've used the source to answer "how". And that's really the most important part of the source: the using. Anybody can just find them (though I have to admit I'm really happy that you did). (apologies to Jerry)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.