Switch statement not working with URL parameter passed.

I have a function with a switch statement that works fine if I hardcode the parameter, but does not work if the parameter is set from another web page passed through via the URL.  The function is called from the onload event of the page.  The exact code is listed below in its non-functional state.  The code can be made to work by changing how the "scategory" variable is set--comment out the first line and uncomment the second line.  What is going on with the URL parm that is different from its hardcoded breathren?  If you look closely, you will notice that I echo the parm when it fails.  It looks correct, but obviously is not somehow.  Please, is there someone that can tell me the difference?

Thank you!


var scategory;

function GetCategory()
{
      
      scategory = location.search.substr(1).split("?");
      //scategory = "Red";
      
      switch (scategory)
      {
      case "Red":            
            objSubmenu=document.getElementById('gallery_maintext')
            objSubmenu.innerHTML = "Red passed in"
            break
      case "Blue":
            objSubmenu=document.getElementById('gallery_maintext')
            objSubmenu.innerHTML = "Blue passed in"
            break      
      default:
            objSubmenu=document.getElementById('gallery_maintext')
            objSubmenu.innerHTML = "<p align='center'><strong>It's still not working!</strong></p> <br>" + scategory
      }            
      
}
cstumneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KennyTMCommented:
Hi. What's the form of your URL?

But anyway after split()-ing the returned value is an Array, not a String like "Red", "Blue", etc. Maybe try this:

     scategory = location.search.substr(1);
0
WelkinMazeCommented:
Hi,
You can see here about the split()
http://www.pageresource.com/jscript/jstring2.htm

The use of location.search.substr(1).split("?"); seems a little strange. As KennyTM has written the URL really can help.
0
cstumneAuthor Commented:
the URL looks like:

http://www.domain.com/page.htm?Red

When I echo the scategory variable into the innerHTML it looks like I would expect it to.  Perhaps that it is an array I need to reference the array element, ie) scategory(0)?  Since the construct is an array, the switch statement (and for that matter if statements) will not recognize the individual element string?   I will try this and get back to you.   Thanks!
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

WelkinMazeCommented:
Yes, you have to use either
location.search.substr(1);
or
scategory[0];     -> the brackets are [] not ()
0
KennyTMCommented:
to reference an array element, you use

scategory[0]

(square brackets instead of parenthesis)

However there's no need to make an array from this because you're only passing a single variable. So

    scategory = location.search.substr(1);

would be enough.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
radnorCommented:
Do you have PHP available????   If so, this WILL work....  Call it with a URL like this:
http://www.abc.com/index.php?Id=blue 

<html>
<head>
<title>get URL parameter</title>
</head>
<body>

<script>
      strURLVariable = <?php echo('"'.$_REQUEST[Id]).'";'; ?>

      alert("URL Variable is: " + strURLVariable);
</script>
</body>
</html>
0
radnorCommented:
Don't want to use PHP, fine...  This will return blue"  I have to get the trailing " off....
call it with a URL like you have now:
http://www.abc.com?blue


Dont have time now to kill the trailing QUOTE
I know find string length and output it LESS 1
When I post code, I TEST it.  Dont have time now to put it up

<html>
<head>
<title>get URL parameter</title>
</head>
<body>

<script>
      strURLVariable = '"' + document.location + '"';
      alert(strURLVariable.substr(strURLVariable.indexOf("?")+1));
</script>
</body>
</html>
0
cstumneAuthor Commented:
It was an array and the answers to that fact all worked.  Thanks again--excellent job all of you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.