Last active
February 18, 2016 03:58
-
-
Save lackneets/e945d328838b11eed250 to your computer and use it in GitHub Desktop.
iframe cross-domain auto height
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Send Height to Parent --> | |
<script type="text/javascript"> | |
function sendHeight(){ | |
if(parent.postMessage){ | |
parent.postMessage({ | |
height: document.body.offsetHeight, | |
location: window.location.toString() | |
}, '*'); | |
} | |
} | |
window.addEventListener('resize', sendHeight); | |
window.addEventListener('load', sendHeight); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Make iframe Responsive--> | |
<iframe src="inner.html" frameborder="0" scrolling="no" class="f1" id="a2"></iframe> | |
<script type="text/javascript"> | |
window.addEventListener('message', onResized); | |
function onResized(event){ | |
if(event.data.location){ | |
Array.prototype.slice.call(document.getElementsByTagName('iframe')).forEach(function(frame){ | |
if(event.data.location == frame.src){ | |
if(frame.style.setProperty){ | |
frame.style.setProperty("height", event.data.height + "px", "important"); | |
}else{ | |
frame.style.setAttribute("style", "height:" event.data.height + "px !important"); | |
} | |
} | |
}); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment