i need help with a little code that i have below. i'm a newbie in javascript so i hope someone could help me here
i've used the function below to grab parameters from URL:
<script type="text/javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>
now the problem is that i need to set that parameters into variable so i can use that variable into something else.
for instance i have below form:
<form method="get" action="#" name="dealer">
insert dealer code<br />
<input type="text" name="dcode" />
<input type="submit开发者_开发百科" value="submit" />
</form>
so whenever i pressed submit button, URL will have parameter. i want to use this parameter as variable. here is the output code
var dealer_param = gup( 'dcode' );
document.write(dealer_param);
again for instance i have define variables:
var a1 = "John Doe"
var a2 = "Jane Doe"
now i want to type a1 in the form box and when i pressed submit i want javascript gave me John Doe as the output and not a1 as i'm currently have
i hope it's not much of a confusing question
thanks in advance
~aji
The easiest solution is to store the values in an assocuative array instead of individual variables. E.g.
var arr;
arr['a1'] = "John Doe";
arr['a2'] = "Jane Doe";
// ...
var dealer_param = gup( 'dcode' );
document.write(arr[dealer_param]);
A second approach is to use eval
- which I personally would recommend against for a variety of reasons including security issues.
var a1 = "John Doe"
var a2 = "Jane Doe"
var dealer_param = gup( 'dcode' );
document.write(eval(dealer_param));
精彩评论