Skip to content

Instantly share code, notes, and snippets.

@roman204
Last active June 7, 2020 11:20
Show Gist options
  • Save roman204/6e28eeabaac693142cd1fbc5ba7c6ca1 to your computer and use it in GitHub Desktop.
Save roman204/6e28eeabaac693142cd1fbc5ba7c6ca1 to your computer and use it in GitHub Desktop.
POC for sending data to parent window when the document is embedded via iframe, it uses the postMessage function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// Get height of document
function getDocHeight(doc) {
doc = doc || document;
// from http://stackoverflow.com/questions/1145850/get-height-of-entire-document-with-javascript
var body = doc.body, html = doc.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
// send docHeight onload
function sendDocHeightMsg(e) {
var ht = getDocHeight();
parent.postMessage( JSON.stringify( {'docHeight': ht} ), 'https://configurator.plissee.com' );
}
// assign onload handler
if ( window.addEventListener ) {
window.addEventListener('load', sendDocHeightMsg, false);
} else if ( window.attachEvent ) { // ie8
window.attachEvent('onload', sendDocHeightMsg);
}
</script>
<button onclick="sendDocHeightMsg()">klick me</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment