Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript passing event from one window to another

Tags:

javascript

Is there any way to open a new window with,

var newWindow = window.open(...);

and then pass events from newWindow to its opener window?

What I want to do is open a window that asks for some information, then once it's been entered close the new window and trigger an action in the original window.

Thanks.

edit: Thanks all. That's pretty much how I thought it worked, but I must be doing something stupid. Here's some test code I've been banging my head against a wall over:

in parent.html

window.open("child.html");

$(window).bind("something", function(e) {
    console.log('something happened');
});

and in child.html

$("#somebutton").click(function() {
    $(window.opener).trigger("something");
    window.close();
});

child opens fine, I click the button and child closes, but "something" never happens in parent!?

I'd kind of like it to work the other way around too. Any way to make something like this work?

var child = window.open("child.html");

$(child).bind("something", function() { ... });

Thanks.

like image 881
nicholas Avatar asked Sep 08 '11 01:09

nicholas


People also ask

Can I pass a JavaScript variable to another browser window?

Can I pass a JavaScript variable to another browser window? Passing variables between the windows (if your windows are on the same domain) can be easily done via: Cookies. localStorage.

How do I pass onClick event?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How browser handles events?

Events are signals fired inside the browser window that notify of changes in the browser or operating system environment. Programmers can create event handler code that will run when an event fires, allowing web pages to respond appropriately to change.

How to use window addEventListener?

The addEventListener() methodYou can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object. The addEventListener() method makes it easier to control how the event reacts to bubbling.


3 Answers

Yes, you can do this. In your new window, you can access the parent window by using window.opener This gives you access to all properties and function of the parent window so you can do things like this for example: window.opener.someFunctionOfMainWindow('some data from new window');

In your case you can do something like this in the new window.

function dataIsEntered() {
    window.opener.triggerAction(data);
    window.close();
}
like image 118
Brian Glaz Avatar answered Oct 15 '22 01:10

Brian Glaz


I hesitate to plug jQuery unnecessarily, but it is quite nice for things like this:

$('#somebutton').click(function() { window.opener.$('body').trigger('someevent', somedata); });

Just run that line in the initialization script of the child window. Note: the even must obviously be bound to the body element in the parent.

Note that you are limited by single-origin policy here. Unless explicitly permitted by the browser, this will only work if both windows are opened from the same host.

like image 28
Dan Avatar answered Oct 15 '22 00:10

Dan


Indeed it is possible, you want to look into window.addEventListener. This is usable in the modern browsers, as well as IE 9.

Visit this page for more information and examples.

like image 40
ShaneC Avatar answered Oct 15 '22 02:10

ShaneC