Forum Moderators: martinibuster

Message Too Old, No Replies

googletag.cmd.push(function() { } not loading

         

csdude55

8:38 pm on May 24, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm hoping one of you geniuses can help me figure this out :-)

I'm loading DFP tags inside of a loop. For some reason, the first iteration of the loop doesn't seem to load googletag.cmd.push(function() {...}, but then the rest do, and I'm at a loss as to why this is happening.

Here's my code. I'm including a lot just in case you guys see something out of place, but I think the only part that's actually relevant is function showData(data_b) {...}:

// This is probably irrelevant, but just in case... I do have a header banner
// that loads, so I'm confident nothing here is messed up
<script async src="https://www.googletagservices.com/tag/js/gpt.js"></script>
<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];

var slot = new Array();

googletag.cmd.push(function() {
googletag.pubads().enableSingleRequest();
googletag.pubads().disableInitialLoad();

googletag.enableServices();
});
</script>

// This is the part that is relevant...
<script>
function showData(data_b) {
var n = 2;
if (data_b == 2) n = 1;

for (i=1; i <= n; i++) {
if (('#data_b_' + data_b).length) {

// This shows up in all iterations
$('#data_b_' + data_b).append(data_b);

googletag.cmd.push(function() {

// This does NOT show up on data_b = 2, but shows up on all others. So
// for some reason, googletag.cmd.push() fails on the first iteration
$('#data_b_' + data_b).append(data_b);

// This doesn't work on data_b = 2 OR data_b = 3, but does on all others
slot[data_b] = googletag.defineSlot('/xxxx/Medium_Rectangle',
[300, 250], 'data_b_' + data_b).
addService(googletag.pubads());

googletag.display('data_b_' + data_b);
googletag.pubads().refresh([slot[data_b]]);
});
}

data_b++;
}
}
</script>

// Then, later in PHP... the first loop is on the page, then the rest are in
// an infinite scroll. So data_b = 2 is embedded on the page, then 3+ is
// in an Ajax fragment. But since 2 is the one NOT working, I didn't include
// the infinite scroll coding
$data_b = 2;

for ($i = 0; $i < 10; $i++) {
if ($i > 0 && ($i / 5 == intval($i / 5))) {
$data_b++;

echo <<<EOF
<div id="data_b_$data_b"></div>
<script>
showData($data_b);
</script>

EOF;
}
}

csdude55

8:49 pm on May 24, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unexpected update... I just discovered that if I put showData() in a setTimeout, everything works correctly:

setTimeout("showData($data_b)", 100);

So it appears that the problem is that data_b_2 isn't being generated by time showData() runs? Even though I'm testing to make sure it exists with if (('#data_b_' + data_b).length) {...}?

Also, I tried using $(function() {...}); which seemed to work once, but then it didn't work again after I refreshed... which is really weird to me, since it seems a lot more logical than setTimeout():

$(function() { showData($data_b); });

Any suggestions on the "right" way to do this? Or, why setTimeout() works consistently (so far) but $(function() {...}); doesn't?