Skip to content

Instantly share code, notes, and snippets.

@khromov
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save khromov/81f4cd9e1fe80bcc8235 to your computer and use it in GitHub Desktop.

Select an option

Save khromov/81f4cd9e1fe80bcc8235 to your computer and use it in GitHub Desktop.
dfp-head.html
<!-- IDG DFP wp_head -->
<script type="text/javascript">
var gptadslots = [];
/* Only load ads on the initial page load, although enquire may try to run the again if user resizes their window */
var dfpIsInitialPageLoad = true;
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
var dfpUnitName = "<?=$DFP_UNIT_NAME?>";
(function() {
var useSSL = 'https:' == document.location.protocol;
var dfpSrc = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
gads.src = dfpSrc;
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
googletag.cmd.push(function() {
googletag.pubads().enableAsyncRendering();
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
/* Fix for some ads that won't resize properly */
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if (!event.isEmpty) {
var id = event.slot.b.d;
var defaultHeight = jQuery('#' + id + ' iframe').attr('height');
/* Add adMarkings */
if (jQuery('#' + id).attr('data-ad-marking') == 'true') {
jQuery('#' + id).addClass('adMarkings');
}
jQuery('#' + id + ' iframe').load(function() {
var frameId = jQuery(this).attr('id');
setTimeout(wp_dfp_resizeAdFrame(frameId, defaultHeight), 500);
});
}
});
function wp_dfp_resizeAdFrame(frameId, defaultHeight)
{
var mxHeight = Math.max(document.getElementById(frameId).contentWindow.document.body.offsetHeight,
document.getElementById(frameId).contentWindow.document.body.clientHeight,
document.getElementById(frameId).contentWindow.document.body.scrollHeight);
if (mxHeight == 0)
{
mxHeight = defaultHeight;
}
document.getElementById(frameId).style.height = mxHeight + 'px';
}
googletag.enableServices();
});
</script>
<!-- End IDG DFP wp_head -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment