使用javascript實現頁面定時跳轉總結篇

來源:互聯網
上載者:User

下面對使用JavaScript實現頁面定時跳轉(也稱倒計時跳轉)做一下總結,各種定時跳轉代碼記錄如下:

(1)使用 setTimeout 函數實現定時跳轉(如下代碼要寫在body地區內)
複製代碼 代碼如下:
<script type="text/javascript">
// 3秒鐘之後跳轉到指定的頁面
setTimeout(window.location.href = "http://3aj.cn/javascript/19.html", 3);
</script>

(2)html代碼實現,在頁面的head地區塊內加上如下代碼
複製代碼 代碼如下:
<!-- 5秒鐘後跳轉到指定的頁面 -->
<meta http-equiv="refresh" content="5;url=http://3aj.cn/javascript/19.html" />

(3)稍微複雜點,多見於登陸之後的定時跳轉
複製代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js定時跳轉頁面的方法</title>
</head>
<body>
<script type="text/javascript">
var t = 10; // 設定跳轉的時間
setInterval("refer()", 1000); // 啟動1秒定時
function refer(){
if (t == 0) {
location = "http://3aj.cn/javascript/19.html"; // 設定跳轉的連結地址
}
document.getElementById('show').innerHTML = "" + t + "秒後跳轉到php程式員教程網"; // 顯示倒計時
t--; // 計數器遞減
}
</script>
<span id="show"></span>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.