Last active
August 29, 2015 14:10
-
-
Save TCotton/7a60d397a9dfeb72efc7 to your computer and use it in GitHub Desktop.
Template page load script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Original Size: 855 bytes gzipped (2.33KB uncompressed) | |
* Compiled Size: 493 bytes gzipped (837 bytes uncompressed) | |
* */ | |
// non-minified | |
(function(window, document) { | |
if (window.Promise) { | |
var loader = '/compiled/assets/js/admin/loaders/editFooter/editFooterLoader.js'; | |
var polyfill = '/compiled/assets/js/vendor/polyfills.js'; | |
var scripts; | |
var scriptLoaderBody; | |
var scriptLoaderHead; | |
var promise; | |
scripts = [ | |
'/compiled/assets/js/vendor/require.js', | |
'/compiled/assets/js/config/admin/config.js', | |
loader | |
]; | |
scriptLoaderBody = function(scriptsX) { | |
// appends scripts to the base of the document body | |
script = document.createElement('script'); | |
script.src = scriptsX; | |
script.type = 'text/javascript'; | |
document.body.appendChild(script); | |
}; | |
scriptLoaderHead = function(scriptsX) { | |
// appends scripts to the top of the document head | |
var script; | |
var stylesheet = document.querySelectorAll('[rel="stylesheet"]'); | |
var stylesheetLength = stylesheet.length - 1; | |
script = document.createElement('script'); | |
script.src = scriptsX; | |
script.type = 'text/javascript'; | |
stylesheet[stylesheetLength].parentNode.insertBefore(script, stylesheet[stylesheetLength]); | |
}; | |
// uses promises to make sure that header and scripts are always | |
// loaded in the right order | |
promise = new Promise(function(resolve, reject) { | |
var request = new XMLHttpRequest(); | |
// fetch header using AJAX | |
request.open('GET', '/compiled/assets/js/admin/templates/common/header.html'); | |
request.onload = function() { | |
if (request.status === 200) { | |
resolve(request.response); // we got data here, so resolve the Promise | |
} | |
}; | |
request.onerror = function() { | |
reject(Error('Error fetching data.')); // error occurred, reject the Promise | |
}; | |
request.send(); //send the request | |
}); | |
promise.then(function(data) { | |
// added contents of header template to the document.head | |
document.head.insertAdjacentHTML('afterbegin', data); | |
}).then(function() { | |
scriptLoaderHead(polyfill); | |
}).then(function() { | |
// after head has been inserted then loaded then attach the three scripts to the bottom of the page | |
var aSetInterval; | |
scriptLoaderBody(scripts[0]); | |
aSetInterval = setInterval(function() { | |
// Need to make sure that require is loaded first | |
if (typeof window.require === 'function') { | |
scriptLoaderBody(scripts[1]); | |
scriptLoaderBody(scripts[2]); | |
clearInterval(aSetInterval); | |
} | |
}, 20); | |
}); | |
} else { | |
alert('Sorry, but this website does not work in your browser'); | |
} | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment