我們常常需要做一種功能,就是將網頁導向其他網頁。
如果一直導向原來的網頁 其實效果類似一直重新整理
那我們就可以透過以下code來幫助我們使用這個功能。
在HTML頁面可以使用meta標籤。
五秒後重新整理一次
<meta http-equiv="refresh" content="5" >
三秒後導向其他網頁
<meta http-equiv="refresh" content="3; url=http://another.com">
那我們在haml裡面就可以這樣編寫
%meta{"http-equiv" => "refresh" ,:content => "3;url=http://another.com"}
那我們也可以透過javascript實現這個功能。兩秒後導向其他網頁
setTimeout(function(){window.location.href ="http://another.com";},2000)
這裡稍微做個區別
導向下一頁是"http://another.com"
window.location = "http://another.com"
此頁替換成"http://another.com"(不會有上一頁)
window.location.replace("http://another.com")
開啟新的視窗,打開網址
window.open("http://another.com")
參考資料來源:
1. stackoverflow
2. 腳本之家
3. w3school
沒有留言:
張貼留言