Link to home
Start Free TrialLog in
Avatar of cstumne
cstumne

asked on

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
      }            
      
}
Avatar of KennyTM
KennyTM

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);
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.
Avatar of cstumne

ASKER

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!
Yes, you have to use either
location.search.substr(1);
or
scategory[0];     -> the brackets are [] not ()
ASKER CERTIFIED SOLUTION
Avatar of KennyTM
KennyTM

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
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>
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>
Avatar of cstumne

ASKER

It was an array and the answers to that fact all worked.  Thanks again--excellent job all of you!