JavaScript Window Location

window.location對象可用於獲取當前頁面地址(url)並把瀏覽器重定向到新頁面。html

window.location 對象可不帶 window 前綴書寫。web

一些例子:瀏覽器

  • window.location.href 返回當前頁面的 href (URL)
  • window.location.hostname 返回 web 主機的域名
  • window.location.pathname 返回當前頁面的路徑或文件名
  • window.location.protocol 返回使用的 web 協議(http: 或 https:)
  • window.location.assign 加載新文檔

window.location.assign() 方法加載新文檔。url

實例

加載新文檔:spa

<!DOCTYPE html>
<html>
<body>

<h1>window.location 對象</h1>

<input type="button" value="加載新的文檔" onclick="newDoc()">

<script>
function newDoc() {
  window.location.assign("http://www.w3school.com.cn")
}
</script>

</body>
</html>
相關文章
相關標籤/搜索