homepage Welcome to WebmasterWorld Guest from 107.21.135.68
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Check to see if variable equals approved values
ocon




msg:4461875
 2:06 am on Jun 6, 2012 (gmt 0)

I'm trying to set a variable equal to the value of an url parameter. If this value does not match one of the approved values then I need to set this variable to a default value.

I'm using or statements right now, but this gets kind of ridiculous when you have many different approved values. Is there some kind of shorthand or better way of doing this?


var lang = [grab value of lang parameter if set];

if(lang!="en" || lang!="es" || lang!="pt" || lang!="ja" || lang!="zh" || ...) lang = "ko";

 

Fotiman




msg:4461885
 3:37 am on Jun 6, 2012 (gmt 0)

You could use a switch statement:

var lang = [grab value of lang parameter if set];
switch (lang) {
case 'en':
case 'es':
case 'pt':
case 'ja':
case 'zh':
lang = 'ko';
break;
default:
lang = 'XX';
break;
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved