How to fix error 'Uncaught SyntaxError: Unexpected identifier'

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

  • 1
    Please elaborate your question. – Gufran Hasan Jan 7 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> – P.k sameera chathuranga Jan 7 at 10:36
  • Please visit this link stackoverflow.com/questions/20737442/… – Gufran Hasan Jan 7 at 10:40

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.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:");';

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.