Skip to content

Instantly share code, notes, and snippets.

@kaichen
Created November 27, 2012 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaichen/4153294 to your computer and use it in GitHub Desktop.
Save kaichen/4153294 to your computer and use it in GitHub Desktop.
// 获得窗口宽高
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);
});
}
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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment