js获取url的get传值函数

function getvl(name) {
   var reg = new RegExp("(^|\?|&)"+ name +"=([^&]*)(\s|&|$)", "i");
   if (reg.test(location.href)) return unescape(RegExp.$2.replace(/+/g, " "));
   return "";
};


用法简单的说一下:

比如url是http://blog.163.com?id=1中id的值代码如下

<script>
getvl('id');   //这样就可以了
</script>

在推荐一个权威的方法: 
推荐使用正则表达式来解析URL,其运行效率更高,代码也更加简洁。


function getArgs(){
var args = {};
var match = null;
var search = decodeURIComponent(location.search.substring(1));
var reg = /(?:([^&amp;]+)=([^&amp;]+))/g;
while((match = reg.exec(search))!==null){
args[match[1]] = match[2];
}
return args;
}.


不过这个函数是不接受参数, 获取的值将是JSON 。

再列一下location的所有属性

属性
href        完整的 URL
protocol        协议
hostname        主机名
host        主机名加端口号
port        的端口号
pathname        当前 URL 的路径部分
search        URL 的查询部分
hash        #开始的锚
原文地址:https://www.cnblogs.com/wang1204/p/6055214.html