利用 Date.getSeconds() 取得當前秒數,對廣告總數取模,得到 0~N-1 的索引,每秒自動切換。適用於靜態頁、無資料庫環境。
var ads = [
{txt:"免費廣告位招租", url:"https://ad.example.com/1", img:"https://img.example.com/1.gif", alt:"贊助商 1", w:468, h:60},
{txt:"聚寶盆廣告網", url:"https://cashbox.com.cn/click?cid=1004321", img:"https://cashbox.com.cn/ad?cid=1004321", alt:"贊助商 2", w:468, h:60},
{txt:"中華廣告網", url:"https://www.chinaads.net/click?100188", img:"https://www.chinaads.net/img?100188", alt:"贊助商 3", w:468, h:60}
];
var idx = new Date().getSeconds() % ads.length;
var ad = ads[idx];
document.write('');
document.write('
');
document.write('');
document.write('
' + ad.txt);
<script src="/js/ads.js"></script>
REDIRECT /click.asp
WIDTH 468
HEIGHT 60
BORDER 0
*
https://img.example.com/1.gif
https://ad.example.com/1
贊助商 1
80
https://cashbox.com.cn/ad?cid=1004321
https://cashbox.com.cn/click?cid=1004321
贊助商 2
40
https://www.chinaads.net/img?100188
https://www.chinaads.net/click?100188
贊助商 3
20
REDIRECT:點擊導向頁,接收 url 參數跳轉。WIDTH/HEIGHT/BORDER:圖片屬性。* 之後每組 4 行:圖片URL、目標URL、替代文字、權重 (0~4294967295)。<% Response.Redirect Request.QueryString("url") %>
<%
Set adRot = Server.CreateObject("MSWC.AdRotator")
Response.Write adRot.GetAdvertisement(Server.MapPath("adrotator.txt"))
%>
<!--#include file="ads.asp"-->
| 維度 | JS 秒數輪播 | MSWC.AdRotator |
|---|---|---|
| 部署複雜度 | 極低 (單一 .js) | 需註冊元件、設定文字檔、建立導向頁 |
| 權重控制 | 均等 (秒數取模) | 支援精細權重 |
| 點擊統計 | 需自行實作 | 透過 REDIRECT 頁面易於記錄 |
| 動態更新 | 修改 .js 即時生效 | 修改文字檔即時生效 |
| 瀏覽器相容 | 全支援 | 伺服端輸出標準 HTML,全支援 |
原文提及的「右鍵彈出廣告」屬惡意行為,違反用戶體驗與廣告聯盟政策,嚴禁使用。現代瀏覽器亦會攔截非使用者主動觸發的 window.open。