2010-08-25 121 views
0

我有兩個按鈕的頁面,一個按鈕將打開下一個功能一個彈出窗口:檢測父點擊按鈕使用javascript

function MM_openBrWindow(theURL,winName,features) { //v2.0 
    my_window=window.open(theURL,winName,features); 
} 

,我需要從窗口(MY-WINDOW14),以檢測當第二個按鈕在父頁面中單擊並引發警報。

我該怎麼做?

在此先感謝。

卡洛斯

回答

0

你會在父窗口中的第二個按鈕的Click事件做my_window.alert("hey")。如果要從子窗口訪問父窗口(例如添加事件),請在子窗口中使用window.opener

button2.onclick = function (e) { 
    my_window.buttonTwoHasBeenClicked(); // this is defined in the child window.. once executed it means the button has been clicked. 
} 

如果你想內定義從事件的子窗口:

// put this in your child window javascript source 
// also change "button2" to whatever your second button's ID is. 
window.opener.getElementById("button2").onclick = function() { 
    alert("button has been clicked on parent window"); 
} 
+0

您好,感謝,但我需要的是,子窗口檢測到父按鈕被點擊後,警報只是看它是否工作。 謝謝 – notforever 2010-08-25 21:43:11

+0

@notforever:是的,我知道。你需要一個點擊處理器,不管它在哪裏定義。在那個點擊處理中,你可以在你的'my_window'引用中調用一個函數讓其他窗口「知道」該按鈕已經被點擊。 – 2010-08-25 21:49:14

+0

對不起,我是一個新手,你可以給我一個例子,我不知道如何從我的窗口引用父按鈕。謝謝 – notforever 2010-08-25 21:54:44

0

你需要使用window.opener把代碼中的子窗口中,像這樣的:

function registerWithParent() { 
    window.opener.registerCallback(myCallback); 
} 

你可以打電話比身體onload事件。

<body onload="registerWithParent()"> 

然後你有這樣的父窗口的JS代碼:

var theCallback; 
function registerCallback(fn) { 
    theCallback = fn; 
} 

那麼你的父HTML是這樣的: