# JavaScript 頁面操作 ### 1. history.go() 載入指定的頁面 ``` // 重整頁面 window.history.go(0); // 前往下一頁 window.history.go(1); // 返回上一頁 window.history.go(-1); ``` ### 2. history.back() 返回上一頁 ``` // 返回上一頁 window.history.back(); ``` ### 3. history.forward() 前往下一頁 ``` window.history.forward(); ``` ### 4. document.referrer 擷取上一頁的網址 - 可以用於開啟分頁之後要跳轉回去 ``` let prevPageUrl = document.referrer; // 轉跳回去 document.location.href = prevPageUrl; ``` ### 5. window.open() 指定開啟頁面 (裡面放網址) ``` window.open("https://www.google.com.tw/") ``` ### 6. location 指定開啟頁面 ``` // 指定開啟頁面 location.href = "https://www.google.com.tw/"; // 指定開啟頁面 location.replace("https://www.google.com.tw/"); // 指定開啟頁面 location.assign("https://www.google.com.tw/"); ``` #### href & replace 的差異 - `href` 瀏覽器**會**記錄歷史連結(可以按上、下一頁) - `replace` 瀏覽器**不會**記錄歷史連結(可以按上、下一頁) ### 7. location 重新整理 ``` // 重整頁面 window.location.assign(window.location.href); // 重整頁面 location.reload(); ``` ### 8. 父頁面跳轉 ``` // 重整父頁面畫面 parent.window.history.go(0); parent.window.location.reload(); parent.window.location = window.location.href; parent.window.location.assign(window.location.href); parent.window.location.replace(window.location.href); // 父頁面跳轉到指定位址 parent.window.location.assign("HTTP://www.google.com"); parent.window.location.replace("HTTP://www.google.com"); ``` --- ##### ヽ(∀゚ )人(゚∀)人( ゚∀)人(∀゚ )人(゚∀)人( ゚∀)ノヽ(∀゚ )人(゚∀゚)人( ゚∀)人(∀゚ )人(゚∀゚)人( ゚∀)人(∀゚ )人( ゚∀)人 ##### 以上 如果註解哪裡有錯誤或有問題,歡迎提出來一起討論~~~~
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up