表單新增完成後想自動跳回列表、或頁面搬移了要導引使用者到新位址,都可以靠 <meta http-equiv="refresh"> 達成,不必寫任何 JavaScript。
<meta http-equiv="refresh" content="2; URL=__APP__/index.php/Select/chaxun">
content 的 2 是等待秒數,URL= 後方是目標位址。加上這行後,頁面會在兩秒後自動前往該網址。
實務上通常會同時顯示一段訊息,讓使用者知道即將跳轉,並提供手動點擊的後路:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<if condition="$id eq 1">
<meta http-equiv="refresh" content="2; URL=__APP__/index.php/Select/chaxun">
{$rows}
<font color="#FF0000">兩秒後自動跳轉!</font>
</if>
<foreach name="rows" item="val">
{$val.id} ------ {$val.name}
<hr>
<a href="__APP__/index.php/Select/chaxun">點擊返回繼續查詢!</a>
</foreach>
</body>
</html>
這段是 ThinkPHP 樣板的語法:<if> 判斷是否為新增後的第一筆資料,是的話才輸出 refresh 指示;<foreach> 則把查詢結果逐列印出,並附上一個手動返回的連結。
<body> 裡,雖然多數瀏覽器仍會解析,但這是不符合規範的寫法,應改放到 <head>。setTimeout(function(){ location.href="新網址"; }, 2000); 效果相同,但可做出更細緻的倒數與取消機制。