Best viewed in Chrome version 22 or better (i.e. Chrome Canary)
Array.prototype.push = '(╯°□°)╯︵ ┻━┻';
iframe body { font-family: "Comic Sans"; } /* LOL */
<iframe seamless src="http://parent.com"></iframe>
Your browser doesn't support seamless iframes.
This slide requires Chrome 22 or better (Chrome Canary).
iframe[seamless]{
background-color: transparent;
border: 0px none transparent;
padding: 0px;
overflow: hidden;
}
window.postMessage
// Parent
var iframe = document.getElementById('the-iframe');
iframe.contentWindow.postMessage('sup', '*');
// Iframe
window.parent.addEventListener('message', function (e) {
var data = e.data;
alert(data); // sup
}, false);
<a href="http://example.com" target="_parent">!<a>
<!DOCTYPE html> <!-- the iframe -->
<html>
<head>
<base href="http://parent.com/" target="_parent">
</head>
<body>
<a href="#boom"/>
<!-- becomes: http://parent.com/#boom -->
<a href="http://disqus.com"/>
<!-- unchanged, still opens in parent -->
</body>
</html>
document.referrer
– not 100%
window.location
+ postMessage
// Parent
var iframe = document.getElementById('the-iframe');
iframe.contentWindow.postMessage(
['referrer', window.location.href], '*'
);
// Iframe
window.parent.addEventListener('message', function (e) {
var eventName = e.data[0],
var data = e.data[1];
switch (eventName) {
case 'referrer': injectBaseElement(data); break;
...
}
}, false);
<div>
<a class="test"></a>
<span class="test"></span>
<iframe src="yerpage.html"></iframe>
</div>
// Parent
function getIframeStyles(iframe) {
var $span = $('<span/>')
.appendTo(iframe.parentNode);
var styles = {
color: $span.css('color'),
fontFamily: $span.css('font-family'),
fontSize: $span.css('font-size')
...
};
$span.remove(); // Cleanup
return styles;
}
// Parent
var iframe = document.getElementById('the-iframe');
iframe.contentWindow.postMessage(
['styles', styles], '*'
);
// Iframe
window.parent.addEventListener('message', function (e) {
var eventName = e.data[0];
var data = e.data[1];
switch (eventName) {
case 'styles': injectStyles(data); break;
case 'referrer': injectBaseElement(data); break;
...
}
}, false);
// Iframe
function injectStyles(styles) {
$(document.body).css(styles);
}
// Iframe
function resize() {
var height = $(html).height();
// Backwards – send message to parent
window.parent.postMessage(['setHeight', height], '*');
}
// Parent
window.addEventListener('message', function (e) {
var $iframe = $('#the-iframe');
var eventName = e.data[0];
var data = e.data[1];
switch (eventName) {
case 'setHeight':
$iframe.height(data);
break;
...
}
}, false);
updateDom(function () {
$('#foo').append('<div>bar</div>');
});
updateDom(function () {
$('#baz').addClass('bigger');
});
function updateDom(callback) {
callback();
resize();
}
// Fires resize function 50ms after *last* call
var resize = _.debounce(function () {
_resize();
}, 50);