HolM
HolM

Reputation: 11

Why doesn't my if-else statement work?

Can anyone see why this does not work:

<script>
  if (url==showbasket.html||order1.html||order2.html||order3.html||order4.html) {
     document.write('<span style="font-family:lucida;font-size:10px;">Hello</span>');
  } else {
     document.write('<span style="font-family:lucida;font-size:30px;">Hello Hello</span>');
  }
</script>

I´m trying to write a script that do this:

IF URL = 1.html or 2.html or 3.html or 4.html THEN 
    write option1 
ELSE 
    write option2 (for all other URL´s)

Upvotes: 0

Views: 671

Answers (3)

Adil
Adil

Reputation: 2112

This code is valid, but it will not do what you want

if (url==showbasket.html||order1.html

"url==showbasket.html" checks if "url" is equal to the "html" attribute of object "showbasket". Since showbasket does not exist, your code will throw an exception.

"||order1.html" means the same, check if the "html" attribute of "order1" object is "true"

Like others have said, what you want to do is :

if ( url == "showbasket.html" || url == "order1.html"

Upvotes: 0

Giann
Giann

Reputation: 3192

I don't think you got your if condition right:

if (url == showbasket.html || url == order1.html || ...

Upvotes: 1

Alex
Alex

Reputation: 7374

if (url == "showbasket.html" || url == "order1.html" || url == "order2.html" || url == "order3.html" || url == "order4.html")

You have to do the check for each url and if it's a string use quotes

Upvotes: 3

Related Questions