Akash Babu
Akash Babu

Reputation: 247

Detect if a click was made in child window

I am using window.open to open a new window in javascript and I want to detect clicks made in child window. Suppose if a click was made in the child window I want to make some changes in the parent window. I'm already storing the handle of the child window like var child = window.open(). I have some code like child.onclick = function() in my parent window but it doesn't seem to work.

Upvotes: 3

Views: 1683

Answers (3)

GPicazo
GPicazo

Reputation: 6676

@kcollins02 has a good answer. If the communication is one-way from the child to the parent. If the communication has to go both ways, I would recommend using jquery events as described in this article: http://www.manasinc.com/communicating-between-two-browser-windows-using-jquery-and-custom-events/

Upvotes: 1

Stephen Rutstein
Stephen Rutstein

Reputation: 105

You can try something like this:

var window = new Window();
var child = window.open();

var childFlag = false;
var onclick = new function() {
    alert ('hello');
    flag = true;
}

child.addEventListener('click', onclick);

var parentClick = function() {
    if(childFlag) {
        // Do something...
    }
}

You would create your parent and child windows as normal. Then when a click occurs within the child window, you'd mark off a flag, that the parent also has access to, to indicate that a click occurred. And then do something within the parent window based on that.

Upvotes: 1

kcollins02
kcollins02

Reputation: 81

I would execute the code from the child window as follows:

window.onclick = window.opener.someFunction

Upvotes: 1

Related Questions