谷歌广告没有出现在IE8中

Google Ads not showing up in IE8

本文关键字:IE8 谷歌      更新时间:2023-09-26

我遇到了一个奇怪的问题,谷歌广告(DFP)没有出现在IE8上(没有测试低于8的IE)。

我正在使用以下代码(jQuery正在使用中)。

/*-- Advertizing --*/
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function(){
    var script = $('<{0}></{0}>'.format('script'));
    script.attr('type','text/javascript');
    script.attr('async','async');
    script.attr('src',document.location.protocol + '//www.googletagservices.com/tag/js/gpt.js');
    $('head').eq(0).prepend(script);
    googletag.cmd.push(function() {
        googletag.defineSlot('/1016203/PG_194x662_Async', [194, 662], 'div-gpt-ad-1320434986666-0').addService(googletag.pubads());
        googletag.defineSlot('/1016203/PG_530x99_Async', [530, 99], 'div-gpt-ad-1320435053303-0').addService(googletag.pubads());
        googletag.defineSlot('/1016203/PG_530x50_Async', [530, 50], 'div-gpt-ad-1320435026691-0').addService(googletag.pubads());
        googletag.pubads().enableSingleRequest();
        googletag.enableServices();
    });
    googletag.cmd.push(function () { googletag.display('div-gpt-ad-1320434986666-0'); });
    googletag.cmd.push(function () { googletag.display('div-gpt-ad-1320435053303-0'); });
    googletag.cmd.push(function () { googletag.display('div-gpt-ad-1320435026691-0'); });       
}());

这是经过测试的,并且在IE9、Chrome、Firefox中正常工作。。。但IE8是唯一的例外。该网站位于photogallery.classiccars.com上。(从DOM树上)看起来,一个IFrame在IE8中部分加载,但只是退出了。

Google的代码使用for (var x in array),当Array.prototype扩展时,它在某些浏览器中会出现问题。

我无法理解他们为什么不使用.length属性进行迭代,或者检查hasOwnProperty,但这似乎是眼前的问题。

由于Backbone.js包含在该项目中,该项目需要Undercore.js,因此我正在调整代码库,以便为该项目使用Underscore.js中的实用程序方法。

//instead of an ES5-Shim extension to Array.prototype.filter (for example)
var ary = [...];
//instead of this...
var results = ary.filter(function(item){...}); //es5
//use this
var results = _.filter(ary, function(item){...}); //underscore.js

请注意,任何编写JavaScript的人都应避免使用for。。除非显式检查hasOwnProperty。这适用于数组和对象。

var ary = [...];
for (var x in ary) {
    if (!ary.hasOwnProperty(x)) continue; //skip inherited properties.
    //your handling here
    ...
}