M1.使用meta元素
<meta http-equiv="refresh" content="5;url=hello.html">
http-equiv="refresh" 是刷新頁(yè)面, 5是指5秒后執(zhí)行刷新操作,url是跳轉(zhuǎn)的目的頁(yè)面地址。
<meta http-equiv="refresh" content="5">這行代碼的意思是只刷新,不跳轉(zhuǎn)。
Meta Refresh Tag自動(dòng)轉(zhuǎn)向法: 由于搜索引擎能夠讀取HTML,而Meta tags也是HTML,所以對(duì)于這種自動(dòng)轉(zhuǎn)向法,搜索引擎能夠自動(dòng)檢測(cè)出來(lái)。因而無(wú)論網(wǎng)站的轉(zhuǎn)向出于什么目的,都很容易被搜索引擎視做對(duì)讀者的誤導(dǎo)而受到懲罰。不過(guò),如果跳轉(zhuǎn)延遲時(shí)間設(shè)置合適,搜索引擎就不會(huì)視之為作弊。 頁(yè)面定時(shí)刷新元標(biāo)識(shí)(Meta Refresh Tag)只能放在HTML代碼的< HEAD>區(qū)里。如代碼所示: “5”是延時(shí)跳轉(zhuǎn)的時(shí)間,單位是秒。如果設(shè)為0,就表示立即跳轉(zhuǎn)。從搜索引擎優(yōu)化的角度出發(fā),一般不希望自動(dòng)轉(zhuǎn)向有延遲。不過(guò),如果是用Meta Refresh標(biāo)識(shí)進(jìn)行轉(zhuǎn)向,一定要注意把延遲時(shí)間設(shè)定成至少10秒以上。
M2.使用script代碼
window.location.href = 'hello.html';立即跳轉(zhuǎn)到hello.html頁(yè)面。
setTimeout("window.location.href = 'hello.html'", 5000);5秒后跳轉(zhuǎn)到hello.html頁(yè)面。
用javascript實(shí)現(xiàn)<big>無(wú)延遲</big>自動(dòng)重定向的好處在于:用戶所訪問(wèn)的目標(biāo)URL不會(huì)保留在用戶瀏覽器的歷史記錄中,如果用戶按返回按鈕返回,則將回到跳轉(zhuǎn)前的網(wǎng)頁(yè),而不是包含javascript自動(dòng)重定向腳本的跳轉(zhuǎn)頁(yè)面,所以不會(huì)出現(xiàn)當(dāng)用戶點(diǎn)擊返回按鈕后返回至重定向頁(yè),然后該頁(yè)自動(dòng)跳轉(zhuǎn)到用戶本來(lái)想離開(kāi)的那個(gè)頁(yè)面的尷尬情形。
倒數(shù)計(jì)數(shù)再跳轉(zhuǎn):
<span id="totalTime">5</span>
<script type="text/javascript"> //M4 倒數(shù)計(jì)時(shí)跳轉(zhuǎn) var totalTime = document.getElementById('totalTime'); var second = totalTime.textContent; //totalTime.innerText; setInterval('redirect()', 1000); function redirect(){ if(second <= 0){ location.href = 'hello.html'; } totalTime.innerText = --second; } </script>
在當(dāng)前頁(yè)面倒數(shù)5個(gè)數(shù)后,頁(yè)面跳轉(zhuǎn)到hello.html。
M3.使用form表單
<form action="hello.html" method="get" name="myform"></form>
<script type="text/javascript"> document.myform.submit(); </script>
會(huì)立馬跳轉(zhuǎn)到hello.html