Polyfill ES6/ES7 features into older browsers (client side)

JS
S
JavaScript

Automatically inject polyfills into the browser's javascript engine functions. This is done by assessing browser capabilities by inspecting the user-agent identifier of the browser. Service created by Financial Times. https://polyfill.io/v2/docs/

1
2// OPTION 1: Load all Polyfills for ES6 
3function loadScript(src) {
4    var script = document.createElement('script');
5    script.async = false;
6    script.src = src;
7    document.head.appendChild(script);
8};
9loadScript('//cdn.polyfill.io/v2/polyfill.min.js');
10loadScript('//app.js');
11
12
13// OPTION 2: Load custom Polyfills         
14var features = [];
15('Promise' in window) || features.push('Promise');
16('IntersectionObserver' in window) || features.push('IntersectionObserver');
17('after' in Element.prototype) || features.push('Element.prototype.after');
18if (features.length) {
19    var s = document.createElement('script');
20    s.src = 'https://polyfill.io/v2/polyfill.min.js?features=' + features.join(',') + '&flags=gated,always&callback=main';
21    s.async = true;
22    document.head.appendChild(s);
23} else {
24    boostrap();
25}
26
27function boostrap() {
28    // start your app
29}
30
31
32// OPTION 3: Load all Polyfills and call a function
33// <script src="https://cdn.polyfill.io/v2/polyfill.min.js?callback=polyfillsAreLoaded" defer async></script>
34function polyfillsAreLoaded() {
35    console.log('Now to do the cool stuff...');
36}

Created on 10/31/2017