js 路由

History 对象属性:
length 返回历史列表中的网址数

History 对象方法:
back() 加载 history 列表中的前一个 URL
forward() 加载 history 列表中的下一个 URL
go() 加载 history 列表中的某个具体页面

//获取当前窗口的Url
//return Url=http://localhost:8080/shopping/buyCart.shtml?skuId=510&amount=1
window.location.href

//获取当前窗口的主机名 例如:http://localhost:8080
window.location.host

//获取当前窗口的端口 例如: 8080
window.location.port

//获取当前窗口的路径 例如: /shopping/buyCart.shtml
window.location.pathname

//获取参数 例如: ?skuId=510&amount=1
window.location.search

//跳出当前窗口,打开新窗口
window.open(url);

//获取当前文档的Url
document.URL

//获取当前网址,如: http://localhost:8080/Tmall/index.jsp
window.document.location.href;

//获取主机地址之后的目录如:/Tmall/index.jsp
var pathName=window.document.location.pathname;
var curWwwPath=window.document.location.href;
var pos=curWwwPath.indexOf(pathName);


document默示的是一个文档对象,window默示的是一个窗口对象,一个窗口下可以有多个文档对象。
所以一个窗口下只有一个window.location.href,然则可能有多个document.URL、document.location.href
window.location.href和document.location.href可以被赋值,然后跳转到其它页面,document.URL只能读不可以写


<script>

$(function(){
// 返回 web 主机的域名,如:http://127.0.0.1:8080/testdemo/test.html?id=1&name=test
var url = window.location.href;
// 返回当前页面的路径和文件名,如:/testdemo/test.html
var pathname = window.location.pathname;
// 返回 web 主机的端口,如:8080
var port = window.location.port;
// 返回所使用的 web 协议,如:http:
var protocol = window.location.protocol;
// 获取参数,如:?id=1&name=test
var search = window.location.search;
// 获取指定参数值
var id = getUrlParam('id'); // 1
var name = getUrlParam('name'); // test
});


function getUrlParam(name) {
// 取得url中?后面的字符
var query = window.location.search.substring(1);
// 把参数按&拆分成数组
var param_arr = query.split("&");
for (var i = 0; i < param_arr.length; i++) {
var pair = param_arr[i].split("=");
if (pair[0] == name) {
return pair[1];
}
}
return (false);
}

</script>

原文地址:https://www.cnblogs.com/xushan03/p/14980971.html