Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created November 26, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/7faa655b42d115c1d6619fd2b0b86178 to your computer and use it in GitHub Desktop.
Save danielhaim1/7faa655b42d115c1d6619fd2b0b86178 to your computer and use it in GitHub Desktop.
Programmatically collapse PubMatic space in empty div if no ads are inside
<script type="text/javascript">
// Site Data
var pubId=;
var siteId=;
var kadId=;
var kadwidth=;
var kadheight=;
var kadtype=1;
var kadpageurl= "";
function getSlotElement(el) {
while (el) {
if (el.className && (el.className.search("ad") !== -1)) return el;
el = el.parentElement;
}
}
var pmExtHlp = {
empty: true,
height: kadheight,
currentSlot: getSlotElement(window.frameElement)
};
if (pmExtHlp.currentSlot) {
pmExtHlp.currentSlot.orgInsertBefore = pmExtHlp.currentSlot.insertBefore;
pmExtHlp.currentSlot.insertBefore = function(){};
pmExtHlp.currentSlot.style.height = "0";
pmExtHlp.currentSlot.style.margin = "0";
pmExtHlp.currentSlot.style.padding = "0";
}
</script>
<script type="text/javascript" src="https://ads.pubmatic.com/AdServer/js/showad.js"></script>
<script type="text/javascript">
(function() {
function adjustIframeHeight(h) {
if (window.frameElement) {
window.frameElement.height = h;
window.frameElement.style.height = h + "px";
}
}
var els = document.getElementsByClassName("PubAdAI");
for (var i = 0; i < els.length; i++) {
for (var y = 0; y < els[i].childNodes.length; y++) {
if (els[i].childNodes[y].clientHeight) { pmExtHlp.empty = false; }
if (els[i].childNodes[y].nodeType === 8) {
if (els[i].childNodes[y].data.search("PubMatic ad tag") !== -1) {
var s = els[i].childNodes[y].data.match(/[0-9]{2,}x[0-9]{2,}/);
if (s) { pmExtHlp.height = s[0].split("x")[1]; }
}
}
}
}
adjustIframeHeight(pmExtHlp.height);
if (pmExtHlp.currentSlot && !pmExtHlp.empty) {
pmExtHlp.currentSlot.style.height = "";
pmExtHlp.currentSlot.style.margin = "";
pmExtHlp.currentSlot.style.padding = "";
pmExtHlp.currentSlot.orgInsertBefore(adLabelEl, pmExtHlp.currentSlot.firstChild);
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment