Skip to content

Instantly share code, notes, and snippets.

@takahiro47
Forked from techieshark/copy-into-browser.js
Created August 11, 2016 04:03
Show Gist options
  • Save takahiro47/3e2ca5641b2e65a9d0afd94e35b00b4e to your computer and use it in GitHub Desktop.
Save takahiro47/3e2ca5641b2e65a9d0afd94e35b00b4e to your computer and use it in GitHub Desktop.
jquery-and-lodash-in-console
// For quickly trying things out in the browser, jQuery and Lo-Dash (like Underscore.js) are great.
// Read the code below (never copy & paste code you don't trust),
// then you can copy & paste it into the browser console to load jQuery & lodash.
(function () {
var jq = document.createElement('script');
jq.src = 'https://code.jquery.com/jquery-2.1.4.js';
document.getElementsByTagName('head')[0].appendChild(jq);
// jQuery should be available via "jQuery" (and we'll set up the $ alias in the defer() function)
var ld = document.createElement('script');
ld.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js';
document.getElementsByTagName('head')[0].appendChild(ld);
// lodash should be available via "_".
var defer = function () {
if (window.jQuery) {
jQuery.noConflict();
$ = jQuery;
// jQuery should be available via "$".
} else {
setTimeout(function () {
defer();
}, 50);
}
};
defer(); // wait for jQuery to download, then set up $ alias
console.log('jQuery ($) and lodash (_) should now be available in your browser console.');
console.log('If not, there may be a Content Security Policy error.');
console.log('See: https://jquery.com https://lodash.com for documentation.')
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment