Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

var value in a switch statement 1

Status
Not open for further replies.

Mariootje

Programmer
Aug 10, 2001
33
NL
Hi there,

Problably an easy problem, but could anybody tell me why I am not able to fill the switch-statement with an "function", while I am able to insert a single variable value?

The code I have is as follow:
<html>
<head>
<script type=&quot;text/javascript&quot;>
function browserversion()
{
browser=navigator.appVersion
var txt=browser.match(&quot;3.&quot;)
/*var txt = &quot;3.&quot; when I use this variable the code works I want When I test the var txt with a document.write, in both cases I get the value 3, that why I don't get it!*/
switch(txt)
{
case &quot;2.&quot;:
document.write(&quot;Your Browser is from the stone-age.&quot;);
break;
case &quot;3.&quot;:
document.write(&quot;You should update your Browser.&quot;);
break;
case &quot;4.&quot;:
document.write(&quot;Your Browser is good enough.&quot;);
break;
default:
document.write(&quot;Browserversion not known&quot;);
}
}
</script>
</head>

<body onload=&quot;browserversion()&quot;>

</body>
</html>
 
Hello Mariootje,

My Javascript reference manual says that the argument for the match() method is a regular expression. I never use them myself, but it seems that /3./ might be the regular expression you want. Then there is the value returned by match() which is an array of strings that match the regular expression, which in this case would be 3., or null, or an array of strings depending on the browser.

You could get the browser level as a number and convert it to a string for use in the switch/case with these statements.
Code:
var levelNumber = parseInt(navigator.appVersion);
var levelText = levelNumber.toString(10);

Thanks to O'Reilly Dynamic HTML:The Definitive Reference by Danny Goodman.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top