Skip to content

Instantly share code, notes, and snippets.

@morr
Created March 11, 2012 08:36
Show Gist options
  • Save morr/2015621 to your computer and use it in GitHub Desktop.
Save morr/2015621 to your computer and use it in GitHub Desktop.
Схема коммуникаций между доменами
<script src="http://readmanga.ru/js/jquery/jquery-1.4.3.min.js"></script>
<h2>inner</h2>
<script>
function notify_parent(message) {
if ('parent' in window && window != window.parent && 'postMessage' in window.parent) {
window.parent.postMessage(message, '*');
}
}
jQuery(function() {
notify_parent('page_change:20');
});
$(window).bind('message', function(e) {
$(document.body).append('<h2>remote message from '+e.originalEvent.origin+': "'+e.originalEvent.data+'"</h2>');
var css_match = e.originalEvent.data.match(/^include_stylesheet:(.*)$/);
if (css_match.length == 2) {
var tag = document.createElement('link');
tag.href = css_match[1];
tag.setAttribute('media', 'screen');
tag.setAttribute('type', 'text/css');
tag.setAttribute('rel', 'stylesheet');
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(tag, s);
}
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<h2>outer</h2>
<script>
$(window).on('message', function(e, data) {
$(document.body).append('<h2>remote message from '+e.originalEvent.origin+': "'+e.originalEvent.data+'"</h2>');
});
$(window).load(function() {
window.frames[0].postMessage('include_stylesheet:http://shikimori.org/assets/common-datauri.css', '*');
});
</script>
<iframe src="http://localhost2:3000/iframe/inner" width="800" height="200">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment