js 获取 url 参数

时间:2024-04-16 18:32:36
/**
* 根据页面地址获取所有参数对象
* @return Object{} 返回所有参数
* ------------------------------
* 根据页面地址获取指定参数对象
* @return String 返回指定参数
* ------------------------------
* 示例: http://xxx/x.json?name=test&id=100
* $.getPageParams() --> { name:"test", id:"100" }
* $.getPageParams("name") --> "test"
* $.getPageParams("id") --> "100"
* ------------------------------
*/
$.getPageParams = function(name){
var params = {};
var string = window.location.search.substr(1);
if(string) {
var array = string.split('&');
$.each(array, function(idx, text){
var words= text.split('=');
params[words[0]] = decodeURIComponent(words[1]);
});
}
return name == null ? params : params[name];
};

  

// 采用正则表达式获取地址栏参数
function GetQueryString(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return decodeURIComponent(r[2]); return null;
}

window.location

js 获取 url 参数

 console.log(window.location.href);
console.log(window.location.protocol +"//"+ window.location.hostname +":"+ window.location.port + window.location.pathname);