public
Created

  • Download Gist
js_in_container.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// 获得窗口宽高
function getWindowWidthAndHeight() {
var width = window.innerWidth,
height = window.innerHeight;
 
if (width && typeof(width) == "number" &&
height && typeof(height) == "number") {
return [width, height];
}
if (document.body && document.body.offsetWidth) {
width = document.body.offsetWidth;
height = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
width = document.documentElement.offsetWidth;
height = document.documentElement.offsetHeight;
}
return [width, height];
}
 
// 发出窗口Resize通知
function postResizeMessage(width, height) {
document.getElementById("ifrm").contentWindow
.postMessage([width,height], "http://report.stage.lanshizi.com")
}
 
window.onload = function () {
var wah = getWindowWidthAndHeight()
, orgWidth = wah[0]
, orgHeight = wah[1]
, timer;
 
postResizeMessage(wah[0], wah[1])
 
var bindEvent = function (event, handler) {
if (window.addEventListener) {
window.addEventListener(event, handler, false)
} else {
window.attachEvent("on" + event.toString(), handler)
}
}
 
bindEvent("message", function (event) {
var msg = document.createElement("p")
, text = document.createTextNode("\"" + event.data + "\" from " + event.origin)
msg.appendChild(text)
document.body.appendChild(msg)
})
 
bindEvent("resize", function(){
timer = setTimeout(function () {
wah = getWindowWidthAndHeight()
if (orgWidth != wah[0] || orgHeight != wah[1]) {
clearTimeout(timer)
orgWidth = wah[0]
orgHeight = wah[1]
postResizeMessage.apply(null, wah)
}
}, 101);
});
}
js_in_iframe_content.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
var bindEvent = function (event, handler) {
if (window.addEventListener) {
window.addEventListener(event, handler, false)
} else {
window.attachEvent("on" + event.toString(), handler)
}
}
 
bindEvent("message", function (event) {
var msg = document.createElement("p")
, text = document.createTextNode("\"" + event.data + "\" from " + event.origin)
msg.appendChild(text)
document.body.appendChild(msg)
event.source.postMessage("got it", "http://test.easyread.ly");
})
 
window.onload = function () {
console.log("window.top")
window.top.postMessage("ha", "http://test.easyread.ly");
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.