0

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,

2
  • According to Google 'Sandboxed JavaScript is based on ECMAScript 5.1. Some ECMAScript 6 features such as arrow functions and const/let declarations are available.' - developers.google.com/tag-manager/templates/…. But it looks like they aren't allowed when you publish. The only robust solution is not to use them I guess, or can you just target EcmaScript 6 when you publish? Commented Aug 18, 2020 at 7:47
  • @Toby, that goes only for custom templates. Custom HTML tags do not support ES6 at all. Commented Aug 18, 2020 at 8:35

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly! Thanks for the resolution and feedback Mark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.