Event.ClientX在firefox和chrome中不起作用

时间:2012-04-06 15:06:06

标签: javascript

我的编码在IE中工作但在firefox和chrome中没有...

 function handleWindowClose() {
            if ((window.event.clientX < 0) || (window.event.clientY < 0))
             {
                 event.returnValue = "Are You sure to leave this page";
             }
         }
         window.onbeforeunload = handleWindowClose;

任何人都可以帮助我......

2 个答案:

答案 0 :(得分:8)

window.event仅限IE浏览器。 要使其在其他浏览器中工作,您必须将事件作为处理函数的参数:

function handleWindowClose(e) {
    e = window.event || e; 
        if ((e.clientX < 0) || (e.clientY < 0))
        {
            e.returnValue = "Are You sure to leave this page";
        }
}
window.onbeforeunload = handleWindowClose;

答案 1 :(得分:1)

也许只需添加mousemove处理程序,它将鼠标位置存储在变量

var mouse;
function storeMouse(e)
{
    if(!e) e = window.event;
    mouse = {clientX: e.clientX, clientX:e.clientY};
}
function test(e){
     alert(mouse.clientX);
}

并使用jquery?

  $(window).bind('beforeunload', function() {
    if (iWantTo) {
        return 'Are You sure to leave this page';
    }
});