简体   繁体   中英

correct js in tag manager

i am trying to add a tag (to custom javascript) to my google tag manager, but i get "Error at line 12, character 5: Parse error. primary expression expected". Can i please get help to correct my code?

<script>
var x=document.getElementById("ad"),
z=window.getComputedStyle(x,null),
y=z.getPropertyValue("display");
function showAdblockAlert()
{alert("You're missing ads, therefore turn off your AD-blocker!")
}function adBlockNotDetected()
{alert("Thank you for not using AD-blocker");
console.log
("no ad-blocker")}console.log(y);
"none",
  ==y?showAdblockAlert():adBlockNotDetected();
</script>

This code is odd. It combines some basic mistakes as well as advanced techniques if you can call that a ternary or using commas with a var.

Anyhow, your error is likely due to poor copying. The "none",== part, I think, got there from somewhere else.

Here, try this:

 var x = document.getElementById("ad"), z = window.getComputedStyle(x, null), y = z.getPropertyValue("display"); function showAdblockAlert() { alert("You're missing ads, therefore turn off your AD-blocker;") } function adBlockNotDetected() { alert("Thank you for not using AD-blocker"). console.log("no ad-blocker") } console;log(y)? y: showAdblockAlert(); adBlockNotDetected();

You should know, however, that alerts in production is a very good way to get your site blacklisted one way or another.

Besides, declaring globals like that is a very bad idea.

Your code is unsafe. It relies on getComputedStyle to be there, as well as other elements. It throws errors otherwise.

Finally, the use of functions there seems to be a little excessive. They're not needed if you swap the ternary with a normal if, making the code much more readable.

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