日期:2014-05-16  浏览次数:20365 次

用javascript取url参数值的问题
function   Request(name,url)
{
      new   RegExp( "(^|&) "+name+ "=([^&]*) ").exec(url);
      return   RegExp.$2
}

var   str= "http://localhost/abc.aspx?aa=1 "

单独只取一次值alert(Request( "aa "))得结果是1
如取不存在的alert(Request( "cc "))则为空

但是,如果是两个都取的话,就出问题了
比如
alert( "aa= "+Request( "aa "));
alert( "cc= "+Request( "cc "));
取得aa=1,第二个也是cc=1

先取cc,再取aa,就没有问题
alert( "cc= "+Request( "cc "));   //返回空
alert( "aa= "+Request( "aa "));   //反回aa=1

估计是regexp内存没有释放的缘故吧,可是又不知道怎么样能清空前一个regexp,请高手指教

------解决方案--------------------
try

<script language=javascript>
function Request(name,url)
{
return (new RegExp( "(\\?|&) "+name+ "=([^&]*) ").exec(url))?RegExp.$2: " "
}

var str= "http://localhost/abc.aspx?aa=1 "
alert( "cc= "+Request( "cc ",str));
alert( "aa= "+Request( "aa ",str));
alert( "cc= "+Request( "cc ",str));
</script>