Skip to content

Instantly share code, notes, and snippets.

@glassresistor
Last active December 19, 2015 07:59
Show Gist options
  • Save glassresistor/5922773 to your computer and use it in GitHub Desktop.
Save glassresistor/5922773 to your computer and use it in GitHub Desktop.
Iframes that resize to fit content dynamically across domain. Its great for using 3rd party ads securely and quickly
<html>
<head>
</head>
<body style="margin: 0px;">
<script>
var resize = function(){
window.parent.postMessage(window.document.body.offsetHeight,"*");
}
window.document.body.onload = resize;
window.document.body.addEventListener("DOMNodeInserted", resize);
</script>
<img src="https://www.google.com/images/srpr/logo4w.png" /></br>
<script>
setTimeout(function(){
var p = document.createElement("p");
p.innerHTML = window.location.search;
document.body.appendChild(p);
var p = document.createElement("img");
p.setAttribute('src', "https://www.google.com/images/srpr/logo4w.png");
document.body.appendChild(p);
}, 1000);
</script>
</body>
</html>
<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
</head>
<body>
<iframe height=10px id="test" src="ad.html?p=test&as=asfd" class="iframe" scrolling="no" frameborder="0"></iframe>
<script>
var iF =$('iframe#test')[0];
window.addEventListener("message", function(event){
iF.style.height = event.data + 'px';
}, false);
</script>
</body>
</html>
@glassresistor
Copy link
Author

ad.html is the iframe content to be served from a different domain than parent.html which has the sample of the iframe and script code to be included in your main site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment