在 JavaScript 中開啟或跳轉頁面,常見的做法有三種,它們在行為上各有差異。
window.location = "https://www.xxxxxxxx.net"
這種寫法會在目前的視窗中跳轉,跳轉後仍保留返回(後退)功能。嚴格來說應寫成 window.location.href。
window.location.replace("https://www.xxxxxxxx.net")
同樣是在目前視窗跳轉,但跳轉後不具有返回功能——它會用新頁面取代目前的歷史記錄。
window.open("https://www.xxxxxxxx.net")
會在一個新視窗中打開連結。這種方式一般用於簡單的彈出頁面,不過現在由於大量瀏覽器都會攔截彈出視窗,使用上已大受限制。