JS一千问(15):如何获取当前URL?

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 属性

  1. href  完整网址 (https://www.pkoala.com:8088/index.php#tab3)
  2. protocol 协议架构(http:或 https:)
  3. host 域名 + 端口 (www.pkoala.com:8088)
  4. hostname 域名 (www.pkoala.com)
  5. port 端口号 (8088)
  6. pathname  URL的路径名 (index.php)
  7. search ?后跟的查询字符串 (?id=123)
  8. hash  #后跟的位置标识符 (#tab3)

Leave a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注