简体   繁体   中英

How to control one window from another in HTML5?

I'm trying to control one window from another in HTML5. I'd like it so when I open one window that I log into, and another window that I login to, then from one window I can click a button and something happens in the other window. I'm not sure where to start; can somebody point me in the right direction?

You don't need any HTML5-Features for that but you can easily do it with javascript:

myNewWindow = window.open() opens a new window and assigns a window -object of that new window to myNewWindow , so you can easily access the new window's DOM from the opening script, using the myNewWindow -variable.

It also works the other way: In a script in the new window, you can user window.opener to access the window -object and DOM of the opening window.

Just make sure, that the content of all your windows is loaded from same domain, as javascript does not allow you to control content loaded from another source (refer to "same origin policy" for more information on this topic).

This answer is 6 months late, but for the Googlers (like me):

I would recommend WebSockets for what you're trying to do Jonathan. WebSockets allow the server to broadcast data to any/all browser windows that are open for your page/site in realtime.

Browser support is currently quite poor but there are good shims to get it working across all browsers very efficiently. Socket.io is one such solution which I've used to great success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM