Last active
August 29, 2015 14:08
-
-
Save jimmyjacobson/e4b42ef5cbad789e268c to your computer and use it in GitHub Desktop.
Cross Domain Message Passing
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
/* | |
This code goes in the webpage that is embedded via iframe. | |
Use of JQuery is assumed in this example | |
*/ | |
function postMessage() { | |
var msg = $('body').height(); | |
window.parent.postMessage(msg, '*'); | |
} | |
//Event handler for window resize | |
$(window).resize(postMessage); | |
//Event handler for page load | |
$().ready(postMessage); |
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
/* | |
This code is loaded by the parent webpage. It listens for a message | |
and sets the height of the element with id = id-of-iframe to the value in the message. | |
This is pretty simple code. In a production environment there are many things posting messages | |
so you should be careful about filtering for the specific message from your app. | |
*/ | |
function msgReceiver(ev){ | |
var msg = ev.data; | |
var elem = document.getElementById('id-of-iframe'); | |
if (elem) { | |
elem.style.height = (parseInt(msg)) + "px"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment