javascript 您如何在 2 个浏览器选项卡/窗口之间进行通信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4167965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you communicate between 2 browser tabs/windows?
提问by Justin
I have a web application that launches as second web application in a new browser window / tab. I want to update the content on the original page when the user submits their input on the second page.
我有一个 Web 应用程序,它在新的浏览器窗口/选项卡中作为第二个 Web 应用程序启动。当用户在第二页上提交输入时,我想更新原始页面上的内容。
Real World Example: Google uses this behavior in GMail. When composing a new message if you click the "To" link it brings up a list of your contacts in a new browser window. This leaves the original compose email window open and active for input while the user can at any time select the email recipients from the list of contacts. When they submit that window the selected email addresses are added to the recipient list in the original compose window.
真实世界示例:Google 在 GMail 中使用此行为。在撰写新消息时,如果您单击“收件人”链接,它会在新的浏览器窗口中显示您的联系人列表。这使原始撰写电子邮件窗口保持打开状态并处于活动状态以供输入,而用户可以随时从联系人列表中选择电子邮件收件人。当他们提交该窗口时,选定的电子邮件地址将添加到原始撰写窗口中的收件人列表中。
How is this accomplished? I imagine it could be done using AJAX but ideally the solution will avoid the round trip / programming logic required to route it via the server.
这是如何实现的?我想它可以使用 AJAX 来完成,但理想情况下,该解决方案将避免通过服务器路由所需的往返/编程逻辑。
For reference my technology stack for this is an ASP.NET MVC App launching a second ASP.NET MVC App that contains a Silverlight App. Both applications can / do make use of jQuery.
作为参考,我的技术堆栈是一个 ASP.NET MVC 应用程序,它启动了包含 Silverlight 应用程序的第二个 ASP.NET MVC 应用程序。这两个应用程序都可以/确实使用 jQuery。
Thanks for your help.
谢谢你的帮助。
回答by Amir Raminfar
I don't think you mean two tabs. What Gmail is doing is communicating between a pop up and the parent.
我不认为你的意思是两个标签。Gmail 正在做的是在弹出窗口和父窗口之间进行通信。
So let's say you opened a new window using javascript. In the opened window you can do:
因此,假设您使用 javascript 打开了一个新窗口。在打开的窗口中,您可以执行以下操作:
opener.someFunction()to pass data back. Let me find a solid example and paste it.
opener.someFunction()将数据传回。让我找到一个可靠的例子并粘贴它。
Edit:Here is a good exmample. http://www.javascriptkit.com/javatutors/remote2.shtml
编辑:这是一个很好的例子。http://www.javascriptkit.com/javatutors/remote2.shtml
You can use the opener to do a lot.
您可以使用开瓶器做很多事情。
回答by Darin Dimitrov
You have two possibilities:
你有两种可能:
- Constant polling (setIntervaland AJAX)
- Push (Cometand HTML 5 WebSocket API)
- 常量轮询(setInterval和 AJAX)
- 推送(Comet和HTML 5 WebSocket API)

