简体   繁体   中英

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,

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM