java关闭当前ie窗口,窗口的Java脚本在关闭在IE8

I have implemented some function when the browser will be closed.

window.onbeforeunload = function (evt) {

evt = (evt)? evt:event;

clickY = evt.clientY;

altKey = evt.altKey;

keyCode = evt.keyCode;

if (altKey == true && keyCode == 0){

//Make call to the server to clear the cache

}else if(clickY < 0){

//Make a call to the server to clear the cache

}else {

return;

}

};

Now the problem in the above code is when ever i press enter by clicking in the addressbar or i refreshed the page with refresh button my code gives me alert("21112") which i don't want because the call will trigger only and only if the browser is closed.

So can somebody help me to achieve this.

解决方案

window.onbeforeunload event is calls before your page is unloaded.

There is no method to check if it's refresh or browser window close event.

It fires by different scenarios

Close the current window.

Navigate to another location by entering a new address or selecting a Favorite.

Click an anchor that refers to another document.

Invoke the anchor.click method.

Invoke the document.write method.

Invoke the document.close method.

Invoke the window.close method.

Invoke the window.navigate or NavigateAndFind method.

Invoke the location.replace method.

Invoke the location.reload method.

Specify a new value for the location.href property.

Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.

Invoke the window.open method, providing the possible value _self for the window name.

Invoke the document.open method.

Click the Back, Forward, Refresh, or Home button.

And you can't handle this.

Also note that window.onbeforeunload event supposed to inform user that he leaves page and handler function should return string value that will be included in confirmation popup (except Firefox 4+ see bug).

You can not force the user to stay on the page.

Note: The only thing you can do with onbeforeunload is to notify the user that he is leaving your page

(this is because some evil pages can open another pages and so on... and spam users. This is security restriction).

If you want to make AJAX call before window is unloaded you need to use onunload event. Note that your AJAX call must me synchronus. Asynchronous call will not work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值