Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Created December 6, 2014 03:51
Show Gist options
  • Save Risto-Stevcev/685cb23860e06520a923 to your computer and use it in GitHub Desktop.
Save Risto-Stevcev/685cb23860e06520a923 to your computer and use it in GitHub Desktop.
Dynamically loading javascript
<span class="foo">bar</span>
/* 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