最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to control one window from another in HTML5? - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Feb 8, 2011 at 19:01 Michael Mrozek 175k29 gold badges170 silver badges178 bronze badges asked Feb 8, 2011 at 9:19 Jonathan Ogle-BarringtonJonathan Ogle-Barrington 8142 gold badges11 silver badges29 bronze badges 4
  • 2 HTML5 is a Markup Language to define a document structure. Your question is about program logic. You should provide more relevant information. EG. What do you mean by "log-in to a window"? – Caspar Kleijne Commented Feb 8, 2011 at 9:30
  • 1 you mean this ? – Knu Commented Feb 8, 2011 at 9:37
  • Logging into the window would simply be a way of identifying the session you would be controlling. Ideally i would like to use a button in one browser window with a listener in another window really to control elements in an HTML5 canvas object. Hope that is a little bit more clear, i need to be vague as i cant give away too much sorry – Jonathan Ogle-Barrington Commented Feb 8, 2011 at 9:38
  • I think Knu is on the right lines – Jonathan Ogle-Barrington Commented Feb 8, 2011 at 9:47
Add a comment  | 

2 Answers 2

Reset to default 13

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.

发布评论

评论列表(0)

  1. 暂无评论