0

How to fix error 'Uncaught SyntaxError: Unexpected identifier'

var script = document.createElement("script");
script.type="text/javascript"; script.src = 'url';
script.innerHTML="dataLayer = [ ] ";
document.getElementsByTagName('head')[0].appendChild(script);

3
  • 1
    Please elaborate your question. Jan 7, 2019 at 10:20
  • how to add this script in head tag ,<script>dataLayer=[{"transactionId":"100087965","transactionTotal":1090,"transactionTax":0,"transactionShipping":0,"currencyCode":"LKR","transactionProducts":[{"sku":"ZZ48-XL","name":"White Cold Shoulder Floral Top","category":"Default Category Clothing Tops ","price":1090,"quantity":1}]}];<script>console.log('Debug Objects: </script> Jan 7, 2019 at 10:36
  • Please visit this link stackoverflow.com/questions/20737442/… Jan 7, 2019 at 10:40

1 Answer 1

0

The main issue is in script.innerHTML="dataLayer = [ ] "; line. Actually, you are trying to concatenate string double quote "" but inside the [] array, there is also double quote"" available. So you need to change script.innerHTML= 'dataLayer = [ ]';

I have changed it and it's working now.

var script = document.createElement("script");
script.type="text/javascript";
script.innerHTML='dataLayer=[{"transactionId":"100087965","transactionTotal":1090,"transactionTax":0,"transactionShipping":0,"currencyCode":"LKR","transactionProducts":[{"sku":"ZZ48-XL","name":"White Cold Shoulder Floral Top","category":"Default Category Clothing Tops ","price":1090,"quantity":1}]}];console.log("Debug Objects:");';
document.getElementsByTagName('head')[0].appendChild(script);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.