Skip to content

Instantly share code, notes, and snippets.

@moskinson
Last active December 16, 2015 07:09
Show Gist options
  • Save moskinson/5396749 to your computer and use it in GitHub Desktop.
Save moskinson/5396749 to your computer and use it in GitHub Desktop.
DFP: Detect iframe bring Ads with creative to show or hide empty space for ads Div
<script type='text/javascript'>
$(document).ready(function(){
var checkIframe = setInterval(function bringSecondPhase(){
if (iframeIsLoaded){
clearInterval(checkIframe)
console.log("iframe loaded!")
iframeHasAdvertismentAndShowOrHide()
}
else{
console.log("iframe checking...")
}
},1000)
function iframeIsLoaded(){
var $iframe = $('#div-gpt-ad-AD_UNIT_NAME-1').find('iframe')
return $iframe.length > 0
}
function iframeHasAdvertismentAndShowOrHide(){
var $iframe = $('#div-gpt-ad-AD_UNIT_NAME-1').find('iframe')
if (iframeIsLoaded()){
var $iframeBody = $iframe.contents().find('body')
if (typeof $iframeBody == "undefined" || $iframeBody.html() == ""){
console.log("iframe empty!")
$iframe.width(0)
$iframe.height(0)
$('#advertismentSticker').hide()
}
else {
console.log("iframe with creative!")
$('#advertismentSticker').show()
}
}
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment