JavaScript BOM 窗口位置(window.location)

  • window.location

    window.location对象可用于获取当前页面地址(URL)并将浏览器重定向到新页面。
    window.location对象可以在没有window的前缀被写入。
    • window.location.href 返回当前页面的href(URL)
    • window.location.hostname 返回Web主机的域名
    • window.location.pathname 返回当前页面的路径和文件名
    • window.location.protocol 返回使用的Web协议(http: 或 https: )
    • window.location.assign() 加载新文档
  • window.location.href

    window.location.href属性返回当前页面的URL。
    显示当前页面的href(URL):
    document.getElementById("demo").innerHTML =
    "Page location is " + window.location.href;
    尝试一下
    结果将是:
  • window.location.hostname

    window.location.hostname属性返回Internet主机的名称(当前页面的名称)。
    显示主机名称:
    document.getElementById("demo").innerHTML =
    "Page hostname is " + window.location.hostname;
    尝试一下
    结果将是:
  • window.location.pathname

    window.location.pathname属性返回当前页面的路径名
    显示当前URL的路径名:
    document.getElementById("demo").innerHTML =
    "Page path is " + window.location.pathname;
    尝试一下
    结果将是:
  • window.location.protocol

    window.location.protocol属性返回页面的Web协议。
    显示Web协议:
    document.getElementById("demo").innerHTML =
    "Page protocol is " + window.location.protocol;
    尝试一下
    结果将是:
  • window.location.port

    window.location.port属性返回互联网主机端口(当前页面)的编号。
    显示主机端口:
    document.getElementById("demo").innerHTML =
    "Port number is " + window.location.port;
    尝试一下
    结果将是:
  • window.location.assign()

    window.location.port()方法加载新文档。
    <html>
    <head>
    <script>
    function newDoc() {
      window.location.assign("https://www.jc2182.com")
    }
    </script>
    </head>
    <body>
    
    <input type="button" value="加载新文档" onclick="newDoc()">
    
    </body>
    </html>
    尝试一下