2

I am not a js programmer and I have this issue (Uncaught SyntaxError: Unexpected token else)

on one of our pages ...

I strongly believe this is a duplicate of 2 Questions that were answered around this place:

1) Syntax Error: Unexpected token Else 2) Prompt JavaScript If Else Unexpected Token else

So I admit my sin ... (But) I must verify my case is the same %100 for sure( a trailing ; )

So the script is this:

<script type="text/javascript">
if("0" == 0) var t = 'existing' else var t = 'new';
document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
</script>

in Chrome .... the error shows after the if statement

Community
  • 1
  • 1

4 Answers4

3

You are missing the ; after your command. If you use more than one command in a line you have to end the command with ; It should be:

<script type="text/javascript">
if("0" == 0) var t = 'existing'; else var t = 'new';
document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
</script>

Anyway: Here you see a good reason to use brackets {} instead of the short version

Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
2
if("0" == 0) var t = 'existing'; else var t = 'new';
davegson
  • 8,205
  • 4
  • 51
  • 71
1

Try this:

if("0" == 0) 
{
   var t = 'existing'; 
}
else 
{
   var t = 'new';
}

Although this is very strange that you are comparing "0" == 0

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Maybe its better to create an inline if to set "t" and this also solves your problem

<script type="text/javascript">
        var t = ("0" == 0) ? 'existing' : 'new';
        document.write('<scr'+'ipt type="text/javascript" src="//my.domain.com/tag.js?ck=XXX&Retarget=domain:'+t+':psd:sku:XXXXXXXX"></scr'+'ipt>');
    </script>
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Kapi
  • 547
  • 5
  • 11