Created
August 30, 2012 10:37
-
-
Save codeb2cc/3526005 to your computer and use it in GitHub Desktop.
window.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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<p></p> | |
<div style="border: 1px solid whiteSmoke"> | |
<iframe src="./postMessageB.html" frameborder="0" style="width: 100%;"></iframe> | |
</div> | |
</body> | |
<script> | |
;(function () { | |
var theSong = '白山兮高高,黑水兮滔滔,有此山川之伟大,故生民质朴而雄豪,地所产者丰且美,俗所习者勤与劳,愿以此为基础,应世界进化之洪潮,沐三民主义之圣化,仰青天白日之昭昭,痛国难之未已,恒怒火之中烧,东夷兮狡诈,北虏兮矫骁,灼灼兮其目,霍霍兮其刀,苟捍卫之不力,宁宰割之能逃,惟卧薪而尝胆,庶雪耻于一朝,唯知行合一方为责,无取乎空论之滔滔,唯积学养气可致用,无取乎狂热之呼号,其自迩以行远,其自卑以登高,爱校、爱乡、爱国、爱人类,期终达于世界大同之目标,使命如此其重大,能不奋勉乎吾曹,能不奋勉乎吾曹!' | |
var lyrics = theSong.split(',') | |
var iframe = document.getElementsByTagName('iframe')[0] | |
var sing = function ( song ) { | |
document.getElementsByTagName('p')[0].innerHTML = song || '...' | |
} | |
var chorus = function () { | |
if (lyrics.length) { | |
sing(lyrics.shift()) | |
iframe.contentWindow.postMessage(lyrics.shift(), '*') | |
} | |
} | |
var receiver = function ( event ) { | |
setTimeout(function () { | |
chorus() | |
}, 2000) | |
} | |
window.addEventListener('message', receiver, false) | |
window.onload = chorus | |
})() | |
</script> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<script> | |
;(function () { | |
var sing = function ( song ) { | |
document.getElementsByTagName('p')[0].innerHTML = song || '...' | |
} | |
var receiver = function ( event ) { | |
setTimeout(function () { | |
var lyric = event.data | |
if (lyric) { | |
sing(event.data) | |
event.source.postMessage('...', '*') | |
} | |
}, 2000) | |
} | |
window.addEventListener('message', receiver, false) | |
})() | |
</script> | |
<body> | |
<p> </p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment