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.getEle mentById(' gallery_ma intext')
objSubmenu.innerHTML = "Red passed in"
break
case "Blue":
objSubmenu=document.getEle mentById(' gallery_ma intext')
objSubmenu.innerHTML = "Blue passed in"
break
default:
objSubmenu=document.getEle mentById(' gallery_ma intext')
objSubmenu.innerHTML = "<p align='center'><strong>It' s still not working!</strong></p> <br>" + scategory
}
}
Thank you!
var scategory;
function GetCategory()
{
scategory = location.search.substr(1).
//scategory = "Red";
switch (scategory)
{
case "Red":
objSubmenu=document.getEle
objSubmenu.innerHTML = "Red passed in"
break
case "Blue":
objSubmenu=document.getEle
objSubmenu.innerHTML = "Blue passed in"
break
default:
objSubmenu=document.getEle
objSubmenu.innerHTML = "<p align='center'><strong>It'
}
}
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.
You can see here about the split()
http://www.pageresource.com/jscript/jstring2.htm
The use of location.search.substr(1).
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!
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 ()
location.search.substr(1);
or
scategory[0]; -> the brackets are [] not ()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
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.subst r(strURLVa riable.ind exOf("?")+ 1));
</script>
</body>
</html>
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.subst
</script>
</body>
</html>
ASKER
It was an array and the answers to that fact all worked. Thanks again--excellent job all of you!
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);