Window Location
Window Location Href
location.href 屬性返回當前頁面的 URL。
實例
返回(當前頁面的)整個 URL:
<script>document.write(location.href);</script>
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("http://www.w3cschool.cc") }</script></head><body><input type="button" value="Load new document" onclick="newDoc()"></body></html>
Window History
window.history對象在編寫時可不使用 window 這個前綴。
為了保護用戶隱私,對 JavaScript 訪問該對象的方法做出了限制。
一些方法:
Window History Back
history.back() 方法加載歷史列表中的前一個 URL。
這與在瀏覽器中點擊后退按鈕是相同的:
實例
在頁面上創建后退按鈕:
<html><head><script>function goBack() { window.history.back() }</script></head><body><input type="button" value="Back" onclick="goBack()"></body></html>
Window History Forward
history forward() 方法加載歷史列表中的下一個 URL。
這與在瀏覽器中點擊前進按鈕是相同的:
實例
在頁面上創建一個向前的按鈕:
<html><head><script>function goForward() { window.history.forward() }</script></head><body><input type="button" value="Forward" onclick="goForward()"></body></html>
新聞熱點
疑難解答