js获取url中的参数

获取问号以后的 参数 
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
      return unescape(r[2]);
   return 0;
} 
比如123.com/index.html?a=1&b=2.
GetQueryString("a") 就是1
GetQueryString("b") 就是2




获得#号以后的
hash = window.location.hash;
假如123.com/index.html?a=1&b=2#top
那么hash的值为: #top





可以使用 location 对象属性

hash	设置或返回从井号 (#) 开始的 URL(锚)
host	设置或返回主机名和当前 URL 的端口号
hostname	设置或返回当前 URL 的主机名
href	设置或返回完整的 URL
pathname	设置或返回当前 URL 的路径部分
port	设置或返回当前 URL 的端口号
protocol	设置或返回当前 URL 的协议
search	设置或返回从问号 (?) 开始的 URL(查询部分) 
原文地址:https://www.cnblogs.com/AngryXiaopeng/p/2606976.html