JavaScript中通过使用Location 对象,可获取和设置当前URL,它是Window对象的一个属性,使用其属性获取有关当前页面地址的信息或使用其方法进行一些页面重定向或刷新。
使用代码:
<div id="cur_url"></div>
<div><button onclick="getURL()">获取页面url</button></div>
<script type="text/javascript">
function getURL() {
document.getElementById("cur_url").innerHTML = window.location.href;
}
</script>URL是互联网上的一个地址,由协议、域名和路径组成。
<protocol>//<hostname>:<port>/<pathname><search><hash>window.location 属性
- href 完整网址 (https://www.pkoala.com:8088/index.php#tab3)
- protocol 协议架构(http:或 https:)
- host 域名 + 端口 (www.pkoala.com:8088)
- hostname 域名 (www.pkoala.com)
- port 端口号 (8088)
- pathname URL的路径名 (index.php)
- search ?后跟的查询字符串 (?id=123)
- hash #后跟的位置标识符 (#tab3)