Created
December 6, 2014 03:51
-
-
Save Risto-Stevcev/685cb23860e06520a923 to your computer and use it in GitHub Desktop.
Dynamically loading javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span class="foo">bar</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* So often asked, so here you go! */ | |
var url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'; | |
var xhr = new XMLHttpRequest(); // send the HTTP request for the JS file | |
xhr.onload = function() { | |
eval(this.responseText); // load it by applying eval on the string | |
$('.foo').html('baz'); // jQuery land! | |
} | |
xhr.open('GET', url); | |
xhr.responseType = 'text'; | |
xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment