Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Last active March 27, 2016 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugnburn/6d2770e1403925f31538 to your computer and use it in GitHub Desktop.
Save plugnburn/6d2770e1403925f31538 to your computer and use it in GitHub Desktop.

ESVer: conditionally load your scripts depending on the target platform

ESVer is a tiny and convenient helper script to get you out of the overhead hell for the browsers that don't really need it. It detects whether a browser supports ES6/ES2015 standard natively and allows you to load different script versions depending on the result of this check.

Usage

Include your scripts like this:

<script src="path/to/esver.js" data-es6-src="path/to/modern.js" data-es5-src="path/to/legacy.js"></script>

As the actual target is detected, this declaration gets replaced with an actual <script> element containing the selected script path.

Try to go to the demo from different browsers to see different messages about platform support.

!function(d){
var c = d.currentScript || d.scripts[d.scripts.length - 1], v = 6, ns
try {
v = typeof Symbol() === 'symbol' ? 6 : 5
eval('class X{}')
eval('let a = (x) => x')
}
catch(e) {v = 5}
if(ns = c.getAttribute('data-es'+v+'-src')) d.write('<script src="'+ns+'">\x3c/script>')
c.parentNode.removeChild(c)
}(document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment