A Pen by Mohit Aneja on CodePen.
Created
April 20, 2015 13:41
-
-
Save maneja81/1729f485b027c8f8574a to your computer and use it in GitHub Desktop.
Passing variables between iframe and parent window
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
// Iframe | |
function resize() { | |
var height = $(html).height(); | |
// Backwards – send message to parent | |
window.parent.postMessage(['varName', var_value], '*'); | |
} | |
// Parent | |
window.addEventListener('message', function (e) { | |
var $iframe = $('#the-iframe'); | |
var eventName = e.data[0]; | |
var data = e.data[1]; | |
switch (eventName) { | |
case 'varName': | |
alert(data); | |
break; | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment