简体   繁体   中英

How to refresh page after an event?

I found this similar question but i don't think it solves my problem. For my scenario I have updating / saving of data in my project.

SCENARIO

Filer wants to update his/her record in (PC1)

Then the (PC2) is already open the page for the list of the pending request.

What I want is after every a request from (PC1) the page in (PC2) will automatically refresh if it is currently open - no need for the user to manually reload (eg by pressing F5 ).

You can achieve this by different methods like:

  • long polling: long polling is kinda event driven notifying where sever responds back when there is a change (holds the request until data is available)
  • Short polling - Periodically (for eg, every 30 seconds) hitting the server with ajax request to get fresh data

Both the above techniques are old and not recommended now as you have latest features like HTML5 WebSockets and WebRTC in modern browsers. What you need here is a push from server side whenever there is a change.

I would recommened you to have a look into SignalR ( http://www.asp.net/signalr ) if you are using dotnet backend or if its node backend, then node implementation of signalr ( npmjs.com/package/signalrjs ).

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available. SignalR supports Web Sockets, and falls back to other compatible techniques for older browsers. SignalR includes APIs for connection management (for instance, connect and disconnect events), grouping connections, and authorization.

Update - Just saw an awesome detailed explanation in SO which would give you more insight (may be the question is different, but the answer given is something which would help you) - In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

See How to refresh another page using javascript without opening the same page in a new tab .

Basically what you need to do is:

//somewhere in your code you open a new tab
//maintain a reference to the window
var childWindow = window.open(initialURL);

//anywhere in your code, when you need to change the url of the window
//if you want to refresh that page, simply use newURL = initialURL
child.location.href = newURL;

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