开发者

Controlling one page with another using jQuery

开发者 https://www.devze.com 2023-01-19 13:56 出处:网络
Using two separate browsers or computers, or for the sake of experimentation, two tabs, is there any way to push a command from one page to another that has some sort of listener object?

Using two separate browsers or computers, or for the sake of experimentation, two tabs, is there any way to push a command from one page to another that has some sort of listener object?

So for example:

controller.html has a link, when clicked triggers an action on viewer.html.

Is 开发者_如何学Cthis possible?


Unless one opened the other, and they're on the same domain, this isn't possible. If one did open the other you can communicate with the parent via window.opener and with the child by storing the reference when performing a window.open().


You could implement some sort of messaging though a server. Browser/Computer A sends message to Server. The server cannot send a message to the Browser/Computer B, but the browser can poll the server for new messages.


Your first limitation is that you'll need a server between both ends - sender.html would send an AJAX request to the server, and receiver.html would get the message from the server. The second one is that the HTTP protocol wasn't meant to do server-push, so the server cannot send the message to the receiver unless the receiver initiates a request.

The two approaches for this are polling and long requests.

Polling means the receiver continuously sends AJAX requests to the server, asking if there are any new messages. The server returns any new messages, or tells the receiver that no messages are ready. This is easy to implement, and quite robust, but it produces significant overhead because the receiver is constantly firing requests, most of which are redundant because the answer is "no messages available".

Using the long request approach, the receiver sends a request to the server, but the server doesn't respond until a new message arrives. The upside is that you only ever need one request per new message, but because the request may remain open for a long time, a large number of concurrent connections needs to be managed, putting more strain on the web server.

Yet another alternative is to use a plugin or applet of some sort that can make connections outside the current browser context. Such a plugin can use any protocol it wants, removing the limitations of HTTP in this regard, but there are other limitations.

Personally, I'd go with polling, unless instant response is crucial.


Would a real-time push API help you out in this situation? In most of these cases, a channel is created and when events occur within the channel, they're pushed to any clients within the channel.

Since I'm so new around here, I can only share one link per post. So I've written out a few real-time push APIs that I know of over at pastebin: http://pastebin.com/QcsAWXXC

0

精彩评论

暂无评论...
验证码 换一张
取 消