頁面跳轉是 Web 開發中的基礎操作,本文系統整理了三種主流實現方式,附帶完整代碼示例,供開發者快速參考。
在 <head> 區塊插入以下代碼,頁面停留 0.1 秒後自動跳轉:
<meta http-equiv="refresh" content="0.1; url=https://example.com/">
window.location.href(最常用)window.location.href = "https://example.com/";
self.locationself.location = "https://example.com/";
top.location(適用於框架頁面)top.location = "https://example.com/";
window.navigate(僅 IE 支援,不推薦)window.navigate("https://example.com/");
使用 header() 函數發送 HTTP 302 狀態碼:
<?php
header("Location: https://example.com/");
?>
實務建議:伺服端跳轉(PHP)最可靠,不受瀏覽器設定影響;前端跳轉適用於條件判斷或延遲導向場景。