Skip to content

Instantly share code, notes, and snippets.

@loraxx753
Created November 12, 2018 18:36
Show Gist options
  • Save loraxx753/5e409a51d29489461c8c558be1b1270d to your computer and use it in GitHub Desktop.
Save loraxx753/5e409a51d29489461c8c558be1b1270d to your computer and use it in GitHub Desktop.
Polyfills that will conditionally load based on whether a browser supports promises or not.
const script = document.createElement('script');
const link = document.createElement('link');
function appendThisScriptToTheBodyTag(src, callback, options) {
// Explicitly set the defaults for any optional parameters of the function
options = (options) ? options : new Object()
callback = (callback) ? callback : new Function()
const scriptClone = script.cloneNode(true);
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode
scriptClone.setAttribute('src', src)
for(var key in options) {
scriptClone.setAttribute(key, options[key])
}
document.body.appendChild(scriptClone)
scriptClone.onload = callback
}
function appendThisLinkToTheHeadTag(src) {
const linkClone = link.cloneNode(true);
linkClone.setAttribute('href', src)
linkClone.setAttribute('rel', 'stylesheet')
linkClone.setAttribute('type', 'text/css')
document.head.appendChild(linkClone)
}
(function() {
const polyfills = {
"css": {
"variables": "https://unpkg.com/css-vars-ponyfill@1",
"grid": "https://rawgit.com/FremyCompany/css-grid-polyfill/master/bin/css-polyfills.js"
}
}
/* ☝️ Legend
language: {
//Github url
functionality: url,
...
}
*/
const thisIsALegacyBrowser = !('Promise' in window)
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/serviceWorker#Examples
if (thisIsALegacyBrowser) {
console.log("legacy")
appendThisScriptToTheBodyTag(polyfills.css.variables, function() { cssVars() });
appendThisScriptToTheBodyTag(polyfills.css.grid)
appendThisLinkToTheHeadTag('styles/legacy.css')
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment