頁面跳轉可透過 location、history 或 top 等物件完成。最常見的是設定 window.location.href,也可用 history.back 返回上一頁。
若需要倒數跳轉,可搭配 setTimeout 顯示剩餘秒數,時間到後再導向目標網址。
window.location.href = 'login.jsp';
window.history.back(-1);
self.location = 'top.htm';
top.location = 'xx.jsp';var seconds = 3;
var target = 'index.asp';
var timer = setInterval(function () {
seconds--;
document.getElementById('ShowDiv').innerHTML = '將在 ' + seconds + ' 秒後跳轉';
if (seconds <= 0) {
clearInterval(timer);
window.location = target;
}
}, 1000);