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