JackNesbitt
JackNesbitt

Reputation: 125

JavaScript compiler error - Google Tag Manager

I currently have a script in Google Tag Manager, which when I am going ot publish, is giving me x 2 JavaScript compiler errors

JavaScript compiler error   

Error at line 3, character 7: This language feature is only supported for ECMASCRIPT6 mode or better: const declaration.

Error at line 4, character 18: This language feature is only supported for ECMASCRIPT6 mode or better: arrow function.

Here is my code:

<script>
    (function() {
      const h4 = document.querySelectorAll('.h4');
      h4.forEach(el => {
        el.innerHTML = el.innerHTML.replace(/sweater/gi, 'jumper');
      });
    })();
</script>

This is working correctly in DevTools. Is there a one size fits all resolution for these errors, or is it bespoke depending on your code?

Thanks,

Upvotes: 0

Views: 278

Answers (1)

Mark Baijens
Mark Baijens

Reputation: 13222

I don't know google tag manager. Perhaps you can set that up to support ECMASCRIPT6.

If you want it to work on an older version of ECMASCRIPT then you can replace const with var and use an anonymous function instead of an arrow function.

(function() {
  var h4 = document.querySelectorAll('.h4');
  h4.forEach(function(el) {
    el.innerHTML = el.innerHTML.replace(/sweater/gi, 'jumper');
  });
})();
<h4 class="h4">Black sweater</h4>

Upvotes: 1

Related Questions