字體:  

如何使用 JavaScript 設定幾秒後跳轉頁面

99770 發表於: 2023-2-15 09:45 來源: ADJ網路控股集團


如果希望使用 JavaScript 來跳轉頁面,只需要用location.href即可達成,location接口表示連結的位置。

幾秒後跳轉語法
有時候我們可能會希望在某些交互動作後實現「幾秒後跳轉」的功能,這時候只需要設定setTimeout即可。

頁面上顯示倒數+跳轉頁面

如果希望在瀏覽器上顯示倒數秒數,可以將倒數秒數的結果寫在指定的元素裡,具體作法請參考以下範例。

5秒後跳轉到新網站,會需要建立兩隻檔案, index.html 及 script.js
(1) index.html

QUOTE:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" c>
    <meta name="viewport" c>
    <script src="script.js" defer></script>
    <title>5秒後跳轉到新網站</title>
</head>
<body>
    <main>
        <center>網頁即將在 <span id="timeBox"></span> 後跳轉到新網站</center>
    </main>
</body>
</html>


(2) script.js

QUOTE:


// 設定秒數
var count = 5;
function countDown(){
        // 將秒數寫在指定元素中
        document.getElementById("timeBox").innerHTML= count;
        // 每次執行就減1
        count -= 1;
        // 當 count = 0 時跳轉頁面
        if (count == 0){
                location.href="http://www.adj.idv.tw";
        }
        // 設定每秒執行1次
        setTimeout("countDown()",1000);
}
// 執行 countDown
countDown();


變數說明:
var count:定義變數,變數可自訂。
document.getElementById("timeBox"):找到id為timeBox的元素。
innerHTML:將字串插入指定的元素之HTML中。
x -= y:相當於 x = x - y。
location.href:轉址
setTimeout:設定秒數(單位是毫秒)
("countDown()",1000):每秒執行一次 countDown()


這樣就可以囉!

最新回復

jijenoh362 at 2023-5-12 14:34:06
https://cialistw.neocities.org/levitravardenafil.htm
https://cialistw.neocities.org/futures0913.html
https://cialistw.neocities.org/bladdercancer0927.html
https://cialistw.neocities.org/levitra.html
https://cialistw.neocities.org/kamagrashopee.htm
https://cialistw.neocities.org/tw2.html
https://cialistw.neocities.org/tw1.html
https://cialistw.neocities.org/ordinary0914.htm
https://cialistw.neocities.org/abdominalpain0928.html
https://cialistw.neocities.org/testosterone.htm
https://cialistw.neocities.org/tawinman.htm
https://cialistw.neocities.org/supercialis.html
https://cialistw.neocities.org/stenagra.html
https://cialistw.neocities.org/index.html
https://cialistw.neocities.org/prostatitis0929.html
https://cialistw.neocities.org/preferred0915.html
https://cialistw.neocities.org/google3f5f8dd0cb2a25e3.html
https://cialistw.neocities.org/extrasupertadarad.htm
https://cialistw.neocities.org/etmallcialis.html
https://cialistw.neocities.org/etcshopee.htm
https://cialistw.neocities.org/neurogenic0930.html
https://cialistw.neocities.org/edtreat.html
https://cialistw.neocities.org/treasury0916.html
https://cialistw.neocities.org/12.html
https://cialistw.neocities.org/11.html
https://cialistw.neocities.org/10.html
https://cialistw.neocities.org/strictureofureter0931.html
https://cialistw.neocities.org/1.html
https://cialistw.neocities.org/urineretention.html
https://cialistw.neocities.org/renalstone0917.html
https://cialistw.neocities.org/urine.html
https://cialistw.neocities.org/tw9.html
https://cialistw.neocities.org/disordersofprostate0932.html
https://cialistw.neocities.org/momoshopee.htm
https://cialistw.neocities.org/mansed.htm
https://cialistw.neocities.org/ureteralstone0918.html
https://cialistw.neocities.org/edcenforce.html
https://cialistw.neocities.org/urethritis0933.html
https://cialistw.neocities.org/tw8.html
https://cialistw.neocities.org/tw7.html
https://cialistw.neocities.org/tw6.html
https://cialistw.neocities.org/urinarycalculus0919.html
https://cialistw.neocities.org/tw5.html
https://cialistw.neocities.org/vesicalstone0934.html
https://cialistw.neocities.org/cialis20mg.html
https://cialistw.neocities.org/badliver.html
https://cialistw.neocities.org/apage.html
https://cialistw.neocities.org/contracture0920.html
https://cialistw.neocities.org/tw4.html
https://cialistw.neocities.org/tw3.html