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