pfeds
pfeds

Reputation: 2273

Cross browser-window events?

Is it possible to fire events picked up by a separate browser window? With JavaScript perhaps.

i.e. if my website opened up another window to display summary product information, would it be possible to notify that window to update when a different product is selected in the main window?

Needs to work in IE, but doesn't need to work on Chrome or other browsers.

Upvotes: 2

Views: 1541

Answers (3)

rodneyrehm
rodneyrehm

Reputation: 13557

There are a couple of options.

  1. Cross-Document-Messaging allows you to pass events between windows. For this to work, you need to have a handle of the target window, which you can really only acquire if the target is either an <iframe> (e.g. through window.frames, or window.parent from the iframe's POV) or if the target is a window opened by the current window through window.open() or window.opener from the popup's POV.
  2. Shared Workers can connect otherwise unassociated windows much like Cross-Document-Messaging. Shared Workers are only available in Chrome and Safari, though.
  3. Server-Sent Events could use the server to proxy the communication between your otherwise unassociated windows. This reqires a round-trip to the server (so is not entirely client-based) and is not available in Internet Explorer.
  4. Web Sockets are an option, too. They too suffer from a server round trip and are not available in Internet Explorer. There is socket.io, which polyfills this functionality down to old Internet Explorers - so might be a viable solution.
  5. A hacky solution is abusing LocalStorage. It'll work in modern Browsers and IE8.
  6. jQuery BrowserEvent was something I played with way back when. It abuses the window.event and browsers' local storage capabilites to simulate passing events between browsers. This is nothing you'd want to use in a production environment, though.

German fellows may want to check out Kommunikations-APIs in HTML5 - Welche wann nutzen?

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328574

Use code like this:

Parent window

var func = function() {...}
child = window.open(...)

Child window

window.opener.func(); // Call function in parent window

You can also call function in the child window from the parent but there is one problem: You must wait until the child window has finished loading; window.open() is asynchronous.

More details: Accessing parent window from child window or vice versa using JavaScript

Upvotes: 4

WTK
WTK

Reputation: 16971

Yeah, that would be possible if the window with product information would be open using window.open method in parent window.

That way you can induce communication between those two (without, strictly speaking, events) but with some extra code that's possible.

Upvotes: 0

Related Questions