ADLoader 解決頁面多廣告位載入阻塞問題,支援非同步載入(預設)與同步載入(舊版 IE 強制),整合 OpenX 廣告系統,提供完整前後端解決方案。
ADLoader.js + crapLoader)<?php
/** 廣告載入管理類
* Date: 2013-08-04
* Author: fdipzone
* Ver: 1.0
*/
class ADLoader {
private static $_ads = array();
private static $_step = 300; // 載入間隔 ms
private static $_async = true; // 非同步模式
private static $_config = array();
private static $_jsclass = null;
/** 載入廣告集合
* @param String $channel 管道/版位代碼
* @param Int $step 載入間隔
* @param Boolean $async 是否非同步
* @return String HTML/JS 代碼
*/
public static function load($channel = '', $step = '', $async = '') {
if (isset($step) && is_numeric($step) && $step > 0) self::$_step = $step;
if (isset($async) && is_bool($async)) self::$_async = $async;
// IE 強制同步
if (!self::checkBrowser()) self::$_async = false;
self::getAds($channel);
self::genZoneId();
return self::genHtml();
}
/** 設定配置 */
public static function setConfig($config = array(), $jsclass = '') {
self::$_config = $config;
self::$_jsclass = $jsclass;
}
/** 取得廣告陣列 */
private static function getAds($channel = '') {
$cfg = self::$_config;
self::$_ads = ($cfg && isset($cfg[$channel])) ? $cfg[$channel] : $cfg['default'];
}
/** Zone ID Base64 編碼 */
private static function genZoneId() {
if (!self::$_async) return;
foreach (self::$_ads as &$ad) {
if (isset($ad['zoneId'])) {
$ad['zoneId'] = base64_encode('var zoneid=' . $ad['zoneId'] . ';');
}
}
}
/** 產生輸出 HTML */
private static function genHtml() {
$ads = self::$_ads;
$html = array();
if (self::$_jsclass && $ads) {
$html[] = '<script type="text/javascript" src="' . self::$_jsclass . '"></script>';
if (!self::$_async) {
// 同步模式預載
foreach ($ads as $ad) {
$html[] = '<div id="' . $ad['domId'] . '_container">';
$html[] = '<script type="text/javascript">ADLoader.preload(' . json_encode($ad) . ');</script>';
$html[] = '</div>';
}
}
$html[] = '<script type="text/javascript">';
$html[] = 'var ads=' . json_encode($ads) . ';';
$html[] = '$(document).ready(function(){ ADLoader.load(ads, ' . self::$_step . ', ' . intval(self::$_async) . '); });';
$html[] = '</script>';
}
return implode("\r\n", $html);
}
/** 偵測需強制同步的瀏覽器 */
private static function checkBrowser() {
$ua = $_SERVER['HTTP_USER_AGENT'];
return strstr($ua, 'MSIE') === false;
}
}
?>
<?php
return array(
'case_openx' => array(
array('type'=>'openx', 'domId'=>'ad_728x90', 'zoneId'=>452),
array('type'=>'openx', 'domId'=>'ad_300x250', 'zoneId'=>449),
array('type'=>'openx', 'domId'=>'ad_l2_300x250', 'zoneId'=>394),
),
'case_url' => array(
array('type'=>'url', 'domId'=>'ad_728x90', 'url'=>'adurl.php?zoneid=452'),
array('type'=>'url', 'domId'=>'ad_300x250', 'url'=>'adurl.php?zoneid=449'),
array('type'=>'url', 'domId'=>'ad_l2_300x250', 'url'=>'adurl.php?zoneid=394'),
),
'case_sync_openx' => array(
array('type'=>'openx', 'domId'=>'ad_728x90', 'zoneId'=>452),
array('type'=>'openx', 'domId'=>'ad_300x250', 'zoneId'=>449),
array('type'=>'openx', 'domId'=>'ad_l2_300x250', 'zoneId'=>394),
),
'default' => array(
array('type'=>'openx', 'domId'=>'ad_728x90', 'zoneId'=>452),
array('type'=>'openx', 'domId'=>'ad_300x250', 'zoneId'=>449),
array('type'=>'openx', 'domId'=>'ad_l2_300x250', 'zoneId'=>394),
),
);
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>AD Loader Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.banner1{margin:10px;border:1px solid #ccc;width:728px;height:90px;}
.banner2{margin:10px;border:1px solid #ccc;width:300px;height:250px;}
</style>
</head><body>
<div class="banner1" id="ad_728x90"></div>
<div class="banner2" id="ad_300x250"></div>
<div class="banner2" id="ad_l2_300x250"></div>
<?php
function showAD($channel='', $step='', $async='') {
include('ADLoader.class.php');
$ad_config = include('ADConfig.php');
ADLoader::setConfig($ad_config, 'ADLoader.js');
return ADLoader::load($channel, $step, $async);
}
echo showAD('case_openx'); // 非同步載入
// echo showAD('case_url'); // URL 方式非同步
// echo showAD('case_sync_openx', 300, false); // 同步載入
?>
</body></html>
document.write 攔截、Script 注入、HTML 片段渲染的核心庫(支援 OpenX 等傳統廣告代碼)document.write 且需非同步化新專案建議使用 Google Publisher Tags (GPT) 或 Prebid.js,提供更完善的非同步載入、Header Bidding、延遲載入、視圖可見度監測等功能。