Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

How to conditionally display an element based on value of a URL parameter

I have javascript that successfully gets the value of a URL parameter "location" and makes it available to the page as "loc". Now, if the value of "loc" is "Church Street Storage", I want to display some stuff on the page, otherwise not.

In the code below, the first "document.write" accurately displays "Church Street Storage" so I know the correct value is in "loc". However, the if condition appears to be true all the time ... the "return policy" link displays regardless of the value of "loc".

Seems like this should be simple, but I don't know Javascript so I'm kind of winging it here.

<ul>
<script>document.write(loc);</script>
<script>
if (loc == 'Church Street Storage')
{document.write('<li><a href="/returnpolicy.shtml">Return Policy</a></li>');}
</script>
</ul>

Open in new window

Avatar of Ken Butters
Ken Butters
Flag of United States of America image

your javascript seems to be accurate.

I set loc = to another value and it stopped displaying the return policy Link.

save this as an html file and display it ...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
 <ul>
<script>
loc = 'Church Street Storagex'
document.write(loc);</script>
<script>
if (loc == 'Church Street Storage')
{document.write('<li><a href="/returnpolicy.shtml">Return Policy</a></li>');}
</script>
</ul>
  </body>
</html>

Open in new window

Avatar of alicia1234

ASKER

It displays "Church Street Storagex" but not the "Return Policy" link ...
so what's wrong with my code?  ;-)
ASKER CERTIFIED SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Duh.
You are exactly right. Above the code snippet I gave you, I had

if (loc="Church Street Storage") ... 

Open in new window


Single "=" not "==" so I was actually setting the value of loc to "Church Street Storage" rather than testing for it.

Thank you!