javascript怎么获取链接参数代码实例

javascript如何获取链接参数代码实例

javascript如何获取链接参数代码实例:
使用url传递参数,大家应该不陌生,例如:

http://www.softwhy.com/home.php?mod=space&do=home&view=all

既然传递参数,那么自然就要获得传递的参数,当然获取参数的方式有多种多样,下面就介绍其中的一种,和大家一起分享,希望能够给大家带来一定的帮助,代码如下:

var url="http://www.softwhy.com/home.php?mod=space&do=home&view=all";
if(url.indexOf("?")!=-1) 
{
  var str=url.substr(url.indexOf("?")+1);
  strs=str.split("&");
  for(i=0;i<strs.length;i++) 
  {
    alert(strs.length);
    alert(strs[i].split("=")[0]);
    alert(strs[i].split("=")[1]);
    alert(strs[i].split("=")[0],'=',strs[i].split("=")[1],'<br>');
  }
}

在以上链接中:
1.strs.length的长度是3。
2.strs[0].split("=")[0]是mod,strs[0].split("=")[1]是space。
3.strs[1].split("=")[0]是do,strs[1].split("=")[1]是home。
后面的依次类推。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8822

更多内容可以参阅:http://www.softwhy.com/javascript/