1.判断页面刷新:
window.onbeforeunload = function()
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{
//页面关闭
window.event.returnValue = ""; //这里可以放置你想做的操作代码
}else{
//页面刷新,do something
}
}
2.在后台注册js
function waitThenDoIt(){
try{
if (window.document.readyState){//IE
if (window.document.readyState==’complete’){
doIt();
} else
setTimeout("waitThenDoIt()",10);
} else {//Firefox
window.addEventListener("load",function(){doIt();},false);
}
} catch (ex) {
}
}
try{
if (window.document.readyState){//IE
if (window.document.readyState==’complete’){
doIt();
} else
setTimeout("waitThenDoIt()",10);
} else {//Firefox
window.addEventListener("load",function(){doIt();},false);
}
} catch (ex) {
}
}
function doIt(){
//…
}
将代码中的:
window.addEventListener("load",function(){doIt();},false);
替换为:
window.addEventListener("DOMContentLoaded",function(){doIt();},false);
也可以。
window.addEventListener("load",function(){doIt();},false);
替换为:
window.addEventListener("DOMContentLoaded",function(){doIt();},false);
也可以。