[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Switch statement not working with URL parameter passed.

Posted on 2006-04-19
8
Medium Priority
?
274 Views
Last Modified: 2010-04-06
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
      }            
      
}
0
Comment
Question by:cstumne
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:KennyTM
ID: 16486047
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16487468
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
 

Author Comment

by:cstumne
ID: 16487731
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16487786
Yes, you have to use either
location.search.substr(1);
or
scategory[0];     -> the brackets are [] not ()
0
 
LVL 8

Accepted Solution

by:
KennyTM earned 2000 total points
ID: 16487815
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
 
LVL 8

Expert Comment

by:radnor
ID: 16491989
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
 
LVL 8

Expert Comment

by:radnor
ID: 16492380
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
 

Author Comment

by:cstumne
ID: 16492951
It was an array and the answers to that fact all worked.  Thanks again--excellent job all of you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question