2012-04-06 200 views
0

工作,我有編碼它在IE而不是在火狐和Chrome的工作...Event.ClientX沒有在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; 

誰能幫我...

+0

退房:http://stackoverflow.com/questions/2116177/windows-event-is- undefined-javascript-error-in-firefox – 2012-04-06 15:13:29

回答

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'; 
    } 
}); 
9

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; 
+0

window.event也支持chrome safari和opera。 – Charles 2014-10-09 08:00:53

+1

如果您正在定位一個項目,請不要忘記添加窗口scrollLeft和窗口scrollTop。 – Szabi 2014-11-04 15:29:09