2011-03-30 33 views
0

JavaScript中的mozilla的window.event.clientX的等價性是什麼? 這是我的代碼,函數名爲body->onbeforeunload =「return CloseOrNotClose(event);」mozilla的javascript格式

這適用於ie,但不適用於mozilla。

function CloseOrNotClose(event) { 

var ie = document.all; 
if (ie) { 
      if ((window.event.clientX < 0) || (window.event.clientY < 0)) { 
        return "Your Information related to this exam will be lost and you will have to reappear for it, \nDo you want to continue?"; 
      } 
} 
else { 
    if ((event.clientX < 0) || (event.clientY < 0)) 
    return "Your Information related to this exam will be lost and you will have to reappear for it, \nDo you want to continue?"; 
} 

}

+0

在所有其他的瀏覽器,'event'對象作爲參數傳遞給事件處理程序通過。 – 2011-03-30 10:58:25

回答

2

Internet Explorer具有window.event.clientX

Mozilla有:

function show_coords(event) 
{ 
    var x = event.clientX; 
    var y = event.clientY; 
    alert("X coords: " + x + ", Y coords: " + y); 
} 

EDIT1:

函數調用在身體 - > onbeforeunload =「返回CloseOrNotClose(事件);」

我認爲它僅適用於Mozilla的作爲window.onbeforeunload = CloseOrNotClose;

EDIT2:

注意,在Firefox 4及更高版本返回的字符串不會顯示給用戶。

https://developer.mozilla.org/en/DOM/window.onbeforeunload

+0

抓住jQuery並使用它 - 它確實有助於消除瀏覽器不一致性,並使javascript變得愉快,而不是煩人! – Codecraft 2011-03-30 10:58:38

0

你能告訴你已經使用你的整個功能?

這個工作對我來說:

function getXY(e) 
{ 
    var e=(!e) ? window.event : e; 
    var X = 0; 
    var Y = 0; 

    if(e.pageX) 
    { 
    X = e.pageX + window.pageXOffset; 
    Y = e.pageY + window.pageYOffset; 
    }