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);
-
1Please elaborate your question.– Gufran HasanCommented 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>– P.k sameera chathurangaCommented Jan 7, 2019 at 10:36
-
Please visit this link stackoverflow.com/questions/20737442/…– Gufran HasanCommented Jan 7, 2019 at 10:40
Add a comment
|
1 Answer
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);