Forum Moderators: martinibuster
<script async="async" src="https://www.googletagservices.com/tag/js/gpt.js"></script>
<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
var slot = new Array();
var horizontal_ad;
googletag.cmd.push(function() {
// Mimic a responsive banner
horizontal_ad = googletag.sizeMapping().
addSize([800, 0], [1, 1]). // desktop, > 800x0 don't show anything
addSize([728, 0], [728, 90]). // tablet, > 728x0 show 728x90
addSize([320, 0], [320, 50]). // mobile, > 320x0 show 320x50
build();
// This one shows by default, outside of Infinite Scroll, and works... sometimes.
// In the real code I also have a slot[1] defined for a footer, but it's not relevant
// to this problem so I left it out
slot[0] = googletag.defineSlot('/xxxx/Mobile_Tablet',
[[728, 90], [320, 50]], 'data_b_0').
defineSizeMapping(horizontal_ad).
addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().disableInitialLoad();
googletag.enableServices();
});
</script>
// Set somewhere in the header; $data_b is in PHP
$data_b = 1;
// Later, inside of the Infinite Scroll, using PHP
for ($i = 0; $i < 10; $i++) {
// Show a banner every 5th loop
if ($i > 0 && ($i / 5 == intval($i / 5))) {
$data_b++;
// This code seemed to work, but just the one time. I tried using it on every
// iteration instead of just the first one, but I just had blank ads after that
if ($data_b == 2)
echo <<<EOF
<div id="data_b_$data_b" class="h_banner">
<script>
googletag.cmd.push(function() {
// Identical to slot[0] except for the 'data_b_$data_b'
slot[$data_b] = googletag.defineSlot('/xxxx/Mobile_Tablet',
[[728, 90], [320, 50]], 'data_b_$data_b').
defineSizeMapping(horizontal_ad).
addService(googletag.pubads());
googletag.display('data_b_$data_b');
googletag.pubads().refresh([slot[$data_b]]);
});
</script>
</div>
EOF;
else
// This is based on the information from the second guide, not the Adsense
// support page. It's basically identical, but includes a "newAd" section that I
// don't understand
echo <<<EOF
<div id="data_b_$data_b" class="h_banner">
<script>
// I have no idea what's going on with newAd. This is almost verbatim from the
// sample, but the sample never used the newAd variable after newAd.find(), so
// I don't understand it at all. <div id='data_b_0'> uses slot[0], though, so that's
// what (I think) I'm trying to clone
var newAd = $('#data_b_0').clone().appendTo('#data_b_$data_b').show();
newAd.find('.h_banner').attr('id', 'data_b_$data_b');
// This is identical to the one above
googletag.cmd.push(function() {
slot[$data_b] = googletag.defineSlot('/xxxx/Mobile_Tablet',
[[728, 90], [320, 50]], 'data_b_$data_b').
defineSizeMapping(horizontal_ad).
addService(googletag.pubads());
googletag.display('data_b_$data_b');
googletag.pubads().refresh([slot[$data_b]]);
});
</script>
</div>
EOF;
}
} I'm half convinced that it would work just fine if I went live with it
for ($i = 0; $i < 10; $i++) {
// Show a banner every 5th loop
if ($i > 0 && ($i / 5 == intval($i / 5))) {
$data_b++;
echo <<<EOF
<div id="data_b_$data_b" class="h_banner"></div>
<script>
$('#data_b_$data_b').append('$data_b');
</script>
EOF;
}
} When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed.
[api.jquery.com...]
var data_b = $data_b;
$(listID).append($('<div></div>')
.load($('blah blah blah', function() {
data_b++;
$('#data_b_' + data_b).append(data_b);
})
);