1. 程式人生 > 其它 >Window Location

Window Location

window.location 物件可用於獲取當前頁面地址(URL)並把瀏覽器重定向到新頁面。

Window Location

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 = "頁面位置是 " + window.location.href;

結果是:

頁面位置是 http://www.\...........

Window Location 主機名

window.location.hostname屬性返回(當前頁面的)因特網主機的名稱。

顯示主機的名稱:

document.getElementById("demo").innerHTML = "頁面主機名是 " + window.location.hostname

;

Window Location 路徑名

window.location.pathname屬性返回當前頁面的路徑名。

顯示當前 URL 的路徑名:

document.getElementById("demo").innerHTML = "頁面路徑是 " + window.location.pathname;

Window Location 協議

window.location.protocol屬性返回頁面的 web 協議。

顯示 web 協議:

document.getElementById("demo").innerHTML = "頁面協議是 " + window.location.protocol

;

Window Location 埠

window.location.port屬性返回(當前頁面的)網際網路主機埠的編號。

顯示主機的埠號:

document.getElementById("demo").innerHTML = "埠號是: " + window.location.port;

Window Location Assign

window.location.assign()方法載入新文件。

window location reload

window.location.reload() 方法重新載入頁面