我对JS真的很陌生,我正在努力学习它。
我已经看到了很多关于这个的问题,但似乎没有解决这个问题的唯一答案,也没有明确的代码来实现。
浏览几个 NFL 网站,我发现他们已经实现了一个有趣的代码,我很想知道它是如何工作的,以及是否可以复制它。
他们的 detect-blocked-ads.js 显示了这一点:
//detect if there is an adblock cookie set (has this test been performed before?)
//if not perform the test and save the cookie
$nflcs.detectBlockedAds = function() {
if($nflcs.cookie('adblocker') == null) {
$nflcs('body').append('<div id="adblock-notice">We noticed that you may have an Ad Blocker turned on. Please be aware that our site is best experienced with Ad Blockers turned off. <a href="javascript:void(0);" class="close">[X]</a> <div style="clear:both;"></div></div>');
$nflcs('body').append('<div id="adblock-test" class="bannerAd">This is a test banner</div>');
$nflcs('#adblock-notice a').click(function(e) {
$(this).parent().fadeOut('fast');
});
if((typeof($nflcs('#adblock-test').css('-moz-binding')) != 'undefined' && $nflcs('#adblock-test').css('-moz-binding').indexOf('elemhidehit') !== -1) || ($nflcs('#adblock-test').css('display') == 'none')) {//adblocker in use
$nflcs('#adblock-notice').fadeIn('fast');//show the adblock notice
$nflcs.cookie('adblocker',true,{expires: 3});//save that this test has been performed for 3 days max
}
else {//adblocker not in use
$nflcs.cookie('adblocker',false,{expires: 3});
}
$nflcs('#adblock-test').remove();
}
}
然后,如果启用了广告块,它们会在页面顶部显示一个 CSS 彩色块。
<div style="display: block;" id="adblock-notice">We noticed that you may have an Ad Blocker turned on. Please be aware that our site is best experienced with Ad Blockers turned off. <a onclick='s_objectID="javascript:void(0);_1";return this.s_oc?this.s_oc(e):true' href="javascript:void(0);" class="close">[X]</a> <div style="clear:both;"></div></div>
我很想知道如何调用这个函数来显示附加在 JS 文件中的代码或消息。
谢谢