Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Created March 2, 2016 17:03
Show Gist options
  • Save joshuabaker/4a3297937f1ff75f7e83 to your computer and use it in GitHub Desktop.
Save joshuabaker/4a3297937f1ff75f7e83 to your computer and use it in GitHub Desktop.
Insert a script and creates a global object. Inspired by the Google Analytics tracking code, and other services that don’t care about blocking pages.
(function(window, document, tag, src, object) {
window[object] = window[object] || [];
var script = document.createElement(tag);
script.async = true;
script.src = src;
var firstScript = document.getElementsByTagName(tag)[0];
firstScript.parentNode.insertBefore(script, firstScript);
}(window, document, 'script', '//example.com/path/to/file.js', 'myObject'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment