JavaScript Window Location,javascriptlocation

來源:互聯網
上載者:User

JavaScript Window Location,javascriptlocation
window.location 對象用於獲得當前頁面的地址 (URL),並把瀏覽器重新導向到新的頁面。

Window Location

window.location 對象在編寫時可不使用 window 這個首碼。 一些例子:

一些執行個體:

  • location.hostname 返回 web 主機的網域名稱
  • location.pathname 返回當前頁面的路徑和檔案名稱
  • location.port 返回 web 主機的連接埠 (80 或 443)
  • location.protocol 返回所使用的 web 協議(https:// 或 https://)
Window Location Href

location.href 屬性返回當前頁面的 URL。

執行個體

返回(當前頁面的)整個 URL:

<script>

document.write(location.href);

</script>

以上代碼輸出為:

https://www.runoob.com/js/js-window-location.html


Window Location Pathname

location.pathname 屬性返回 URL 的路徑名。

執行個體

返回當前 URL 的路徑名:

<script>

document.write(location.pathname);

</script>

以上代碼輸出為:

/js/js-window-location.html


Window Location Assign

location.assign() 方法載入新的文檔。

執行個體

載入一個新的文檔:

<html>
<head>
<script>
function newDoc()
  {
  window.location.assign("https://www.w3cschool.cc")
  }
</script>
</head>
<body>

<input type="button" value="Load new document" onclick="newDoc()">

</body>
</html>

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.