Skip to content

Instantly share code, notes, and snippets.

@iainbeeston
Created December 20, 2012 06:39
Show Gist options
  • Save iainbeeston/b225fc3566e4adcc241a to your computer and use it in GitHub Desktop.
Save iainbeeston/b225fc3566e4adcc241a to your computer and use it in GitHub Desktop.
Example code for loading airbrake.js asynchronously
(function(s, id, cb) {
var js, fjs = document.getElementsByTagName(s)[0];
if (document.getElementById(id)) return;
js = document.createElement(s);
js.id = id; js.type = 'text/javascript'; js.async = true;
js.onload = js.onreadystatechang = cb;
js.src = (("https:" == document.location.protocol) ? "https://ssljscdn" : "http://jscdn") + ".airbrake.io/notifier.min.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'airbrake-js-notifier', function () {
Airbrake.setKey('<your-api-key>');
Airbrake.setHost('api.airbrake.io');
Airbrake.setEnvironment('dev');
Airbrake.setGuessFunctionName(true);
Airbrake.setErrorDefaults({
url: document.URL,
component: "hello",
action: "world",
});
}))
@bluesmoon
Copy link

This is asynchronous, but still blocks the onload event handler. The right way to load a script in a non-blocking fashion is described here: http://calendar.perfplanet.com/2012/the-non-blocking-script-loader-pattern/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment