首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

JavaScript 實現頁面隨機自動跳轉的簡單代碼

     編輯:程式設計疑難解答

核心代碼

const urls = [
    "https://www.apple.com",
    "https://www.google.com",
    "https://www.twitter.com"
];

const randomIndex = Math.floor(Math.random() * urls.length);
window.location.href = urls[randomIndex];

進階:定時隨機跳轉

setInterval(() => {
    const idx = Math.floor(Math.random() * urls.length);
    window.location.href = urls[idx];
}, 30000); // 每 30 秒跳轉一次

注意事項

  • 同源策略:只能跳轉到允許的域名或同域頁面
  • 用戶體驗:隨機跳轉極差,僅適用於測試/廣告頁
  • SEO:搜索引擎可能視為惡意跳轉
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved