var url = window.location.href; //获取当前窗口的Url; 结果:http://localhost:61768/Home/Index?id=2&age=18
var host = window.location.host;//获取当前窗口的主机名; 结果:localhost:61768
var port = window.location.port; //获取当前窗口的端口; 结果:61768
var pathname = window.location.pathname;//获取当前窗口的路径 ; 结果:/Home/Index
var URL = document.URL;//获取当前文档的Url;结果:http://localhost:61768/Home/Index?id=2&age=18
var search = window.location.search;//获取参数;结果:?id=2&age=18
分割url提取参数
var search = window.location.search;
var age = getSearchString('age', search); //结果:18
var id = getSearchString('id', search); //结果:2
//key(需要检索的键) url(传入的需要分割的url地址,例:?id=2&age=18)
function getSearchString(key, Url) {
var str = Url;
str = str.substring(1, str.length); // 获取URL中?之后的字符(去掉第一位的问号)
// 以&分隔字符串,获得类似name=xiaoli这样的元素数组
var arr = str.split("&");
var obj = new Object();
// 将每一个数组元素以=分隔并赋给obj对象
for (var i = 0; i < arr.length; i++) {
var tmp_arr = arr[i].split("=");
obj[decodeURIComponent(tmp_arr[0])] = decodeURIComponent(tmp_arr[1]);
}
return obj[key];
}