2021年11月22日星期一

js window.location 信息

 var url; 

url = window.location.href; /* 获取完整URL */ alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */


url = window.location.pathname; /* 获取文件路径(文件地址) */ alert(url); /* /Test/index.html */


url = window.location.protocol; /* 获取协议 */ alert(url); /* http */


url = window.location.host; /* 获取主机地址和端口号 */ alert(url); /* http://127.0.0.1:8020/ */


url = window.location.hostname; /* 获取主机地址 */ alert(url); /* http://127.0.0.1/ */


url = window.location.port; /* 获取端口号 */ alert(url); /* 8020 */


url = window.location.hash; /* 获取锚点(“#”后面的分段) */ alert(url); /* #test?name=test */


url = window.location.search; /* 获取属性(“?”后面的分段) */ alert(url);


/* 如果需要URL中的某一部分,可以自己进行处理 */ 

url = window.location.pathname; 
url = url.substring(url.lastIndexOf('/') + 1, url.length); 
alert(url); /* /index.html */


没有评论:

发表评论

login.js