萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 首頁廣告輪播實作:JavaScript 計時切換與 MSWC.AdRotator 元件兩種方案

方案一:純 JavaScript 秒數輪播 (無元件、跨平台)

原理

利用 Date.getSeconds() 取得當前秒數,對廣告總數取模,得到 0~N-1 的索引,每秒自動切換。適用於靜態頁、無資料庫環境。

完整代碼 (ads.js)

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('' + ad.alt + '');
document.write('');
document.write('
' + ad.txt);

頁面引用

<script src="/js/ads.js"></script>

方案二:ASP 內建 MSWC.AdRotator 元件 (需資料庫/文字檔配置)

1. 建立廣告設定檔 (adrotator.txt)

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)。

2. 導向頁 (click.asp)

<% Response.Redirect Request.QueryString("url") %>

3. 輸出廣告頁面 (ads.asp)

<%
Set adRot = Server.CreateObject("MSWC.AdRotator")
Response.Write adRot.GetAdvertisement(Server.MapPath("adrotator.txt"))
%>

4. 頁面引用

<!--#include file="ads.asp"-->

兩者比較

維度JS 秒數輪播MSWC.AdRotator
部署複雜度極低 (單一 .js)需註冊元件、設定文字檔、建立導向頁
權重控制均等 (秒數取模)支援精細權重
點擊統計需自行實作透過 REDIRECT 頁面易於記錄
動態更新修改 .js 即時生效修改文字檔即時生效
瀏覽器相容全支援伺服端輸出標準 HTML,全支援

安全提醒

原文提及的「右鍵彈出廣告」屬惡意行為,違反用戶體驗與廣告聯盟政策,嚴禁使用。現代瀏覽器亦會攔截非使用者主動觸發的 window.open。

copyright © 萬盛學電腦網 all rights reserved