Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Created March 28, 2013 21:11
Show Gist options
  • Select an option

  • Save jasdeepkhalsa/5266827 to your computer and use it in GitHub Desktop.

Select an option

Save jasdeepkhalsa/5266827 to your computer and use it in GitHub Desktop.
Scouting file by Alex Sexton
// From: http://alexsexton.com/blog/2013/03/deploying-javascript-applications/
// Simplified example of a scout file
(function () {
// Feature test some stuff
var features = {
svg: Modernizr.svg,
touch: Modernizr.touch
};
// The async script injection fanfare
var script = document.createElement('script');
var fScript = document.getElementsByTagName('script')[0];
var baseUrl = '//cool-cdn.com/bucket/' + __BUILDNUM__ + '/';
// Build up a file url based on the features of this user
var featureString = '';
for (var i in features) {
if (features.hasOwnProperty(i) && features[i]) {
featuresString += '-' + i;
}
}
var package = 'build' + featureString + '.js'
// Set the URL on the script
script.src = baseUrl + package;
// Inject the script
fScript.parentNode.insertBefore(script, fScript);
// Start loading the data that you know you can grab right away
// JSONP is small and easy to kick off for this.
var dataScript = document.createElement('script');
// Create a JSONP Url based on some info we have.
// We'll assume localstorage for this example
// though a cookie or url param might be safer.
window.appInitData = function (initialData) {
// Get it to the core application when it eventually
// loads or if it's already there.
// A global is used here for ease of example
window.comeGetMe = initialData;
};
// If we're on a static site, the url might tell us
// the data we need, and the user cookie might customize
// it. Simplified.
dataScript.src = '//api.mysite.com' +
document.location.pathname +
'?userid=' +
localStorage.getItem('userid') +
'&callback=appInitData;';
// Inject it
fScript.parentNode.insertBefore(dataScript, fScript);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment