How to use 'let' (and supported ECMAScript 6 features) in both Firefox and Chrome How to use 'let' (and supported ECMAScript 6 features) in both Firefox and Chrome google-chrome google-chrome

How to use 'let' (and supported ECMAScript 6 features) in both Firefox and Chrome


The naïve solution, assuming you have control over the script itself, is to set some global within the script and check if it exists later. Iff it doesn't, replace this script element with one without the special MIME type so it can run on other browsers. The global can safely be ignored after that.

<script type="application/javascript;version=1.7">  'use strict';  window.fx = true;  let foo = 'bar';  console.log(foo);</script><script>  if (typeof window.fx === 'undefined') {    var oldScript = document.querySelector('script[type="application/javascript;version=1.7"]');    var text = oldScript.text;    document.body.removeChild(oldScript);    var newScript = document.createElement('script');    newScript.text = text;    document.body.appendChild(newScript);  }</script>