有時(shí)我們要對(duì)網(wǎng)頁(yè)做跳轉(zhuǎn),讓用戶打開該頁(yè)面后馬上或是在一定的時(shí)間內(nèi)跳轉(zhuǎn)到另外一個(gè)頁(yè)面,下面小編分享網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)代碼給大家。
動(dòng)跳轉(zhuǎn)代碼方案一,用<meta>里直接寫刷新語(yǔ)句:
如下語(yǔ)句,紅色甩部分改成自己的網(wǎng)頁(yè)地址就好了。藍(lán)色部分為跳轉(zhuǎn)時(shí)間 下面是5秒,可以改成自己需要的時(shí)間,0表示不等待。
<html>
< head>
< meta http-equiv="Content-Language" content="zh-CN">
< meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
< meta http-equiv="refresh" content="5;url=http://m.ametit.com">
< title>html網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)代碼--西農(nóng)大網(wǎng)站</title>
< /head>
< body>測(cè)試:html網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)代碼<br/>
這里可以寫一些文字,在跳轉(zhuǎn)之前可以顯示給用戶!<br />
</body>
< /html>
自動(dòng)動(dòng)跳轉(zhuǎn)代碼方案二,用JavaScript腳本來(lái)跳轉(zhuǎn)
2) javascript的實(shí)現(xiàn)
|
<script language="javascript" type="text/javascript">
// 以下方式直接跳轉(zhuǎn)
window.location.href='hello.html';
// 以下方式定時(shí)跳轉(zhuǎn)
setTimeout("javascript:location.href='http://m.ametit.com'", 5000);
</script>
|
優(yōu)點(diǎn):靈活,可以結(jié)合更多的其他功能
缺點(diǎn):受到不同瀏覽器的影響
3) 結(jié)合了倒數(shù)的javascript實(shí)現(xiàn)(IE)
|
<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
var second = totalSecond.innerText;
setInterval("redirect()", 1000);
function redirect(){
totalSecond.innerText=--second;
if(second<0) location.href='http://m.ametit.com';
}
</script>
|
優(yōu)點(diǎn):更人性化
缺點(diǎn):firefox不支持(firefox不支持span、div等的innerText屬性)
3') 結(jié)合了倒數(shù)的javascript實(shí)現(xiàn)(firefox)
|
<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').textContent;
setInterval("redirect()", 1000);
function redirect()
{
document.getElementById('totalSecond').textContent = --second;
if (second < 0) location.href = 'http://m.ametit.com';
}
</script>
|
4) 解決Firefox不支持innerText的問(wèn)題
|
<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
if(navigator.appName.indexOf("Explorer") > -1){
document.getElementById('totalSecond').innerText = "my text innerText";
} else{
document.getElementById('totalSecond').textContent = "my text textContent";
}
</script>
|
5) 整合3)和3')
|
<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').textContent;
if (navigator.appName.indexOf("Explorer") > -1) {
second = document.getElementById('totalSecond').innerText;
} else {
second = document.getElementById('totalSecond').textContent;
}
setInterval("redirect()", 1000);
function redirect() {
if (second < 0) {
location.href = 'http://m.ametit.com';
} else {
if (navigator.appName.indexOf("Explorer") > -1) {
document.getElementById('totalSecond').innerText = second--;
} else {
document.getElementById('totalSecond').textContent = second--;
}
}
}
</script>
|