![]() |
How to refresh a webpage using a event from another page?
Hello. It is Friday. I thought this will be a simple question, but it seen like hard to find a fast and simple solution. I am working on an online sign in tool. Two browser windows are open at the same time. One is for people to sign in with password and submit their information; another one is for Administrator to see who sign in. I am trying to refresh the Administrator page whenever user finished sign in and submitted their information. These two pages cannot be parent and child page. What will be the easiest way to solve this in ColdFusion or JavaScript? Thank you so much for your time. Have a wonderful weekend.:thumbsup:
|
If you can't have a parent child relationship then you need to form some kind of relationship. The simplest solution that I can think of would be with JavaScript cookies. If both pages have a loop that listens to a cookie value and both pages can write to the cookie, then communication between the two page can be facilitated through the cookie. If the sign in page tells the cookie that it is time for the admin page to refresh, then when the admin page gets that info- it can update itself. That is assuming you want to refresh... if the two pages can communicate so easily, you could just send the information you need to display on the admin page through the cookie.
Your example suggests that the pages might be running on different computers though, is that the case? If so I would recommend a similar solution using ajax to pass the information to the server. |
Quote:
Write a tiny Javascript loop that sends the data you want to see into your machine's localStorage (say, at 1 second intervals). Then have another loop running in the admin page reading and displaying the localStorage data, also looping once a second. It would go like this: [client login] --> localStorage.setItem [admin viewer] <-- localStorage.getItem Haven't tried it, but it should work??? Edit: Just tried it...... it works! |
Thank you so much for the quick response. Can someone help me with coding to create some kind of listening method, or can I just simply send a request from the [client log] to [Admin Viewer]? I know the difficult part is these two pages might be running on different computers? But I will start to try both of your suggestions. Thank you again.
|
If the pages are running on different computers, then you can *NOT* solve this with JavaScript alone.
You *must* have some server-side code (PHP, ASP, JSP, etc.) that receives a message (presumably via AJAX) from one client to store the changed information. Then the Admin page can "scan" (again, using AJAX, and making the request every few seconds) by asking the server what has changed. |
If there is a possibility that they might run on different computers then you need a serverside solution.
It looks like you know ColdFusion and JavaScript both, so using js to write an AJAX loop and coding for the server in ColdFusion is my recommendation, problem is I don't know ColdFusion... You should post that query in the appropriate forum :D As for the server listener, I've been playing around with dynamic script injection for something very similar... I came up with the following to get a simple update from the server to tell the page it needs to refresh, its not AJAX, but I need it to run on a local machine, so this gets around the cross domain scripting issues: Code:
<script id="loadarea" type="text/javascript"></script> |
Blaze: But how do you ensure that the JavaScript that you loaded with your trick then actually gets run? Call a function in that code that you know must exist?
One problem with this is that the loading (assignment to the src) will be *synchronous*. So if there is any hangup in getting the response from the server, the HTML page will be "dead"...no other JS code can run, events, can't happen, etc. That's why people prefer using AJAX. The first A there means "Asynchronous". So while the loading from the server is taking place, all other code on the page is accessible. The completion of the load triggers the onreadystatechange event. If the load hangs, the page keeps running. A variation on your trick might be this: Code:
<iframe style="display: none;" id="loadarea"></iframe>parent.someFunction() or similar. |
Quote:
Quote:
|
Krupski: Read post #4, from the OP.
Quote:
|
Quote:
innerHTML of it into a localStorage object named "test".Then I had another tiny program (same browser, same computer, different "website") reading that same object and writing it's content to the innerHTML of an empty DIV.Both "server" and "client" were running inside a setInterval loop of 1000 msec.I was able to watch the div on the message board "live" with the other window (albeit with a short delay). Of course, this won't work on different MACHINES, but to watch what's going on in one WINDOW inside another WINDOW, it works fine and as I understand it, that's what the OP wanted.... |
Quote:
|
Quote:
|
The full code would have been
index.hta Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Code:
<%@LANGUAGE="JavaScript"%>I *did* say that I definitely recommend AJAX, but the OP asked for a sample of how to set a loop to check the server... this isn't the AJAX forum, and I never claimed my loop was asynchronous... @Old Pedant The problem that I had with your solution (which I definitely tried first!) is that it also runs into cross domain blocking mechanisms in my test environment... no bueno... |
Oh, yes...doesn't work cross-domain. But I don't think that applies in this thread.
|
Quote:
If he is only generating a console to display the actions of the users for example, he might prefer synchronous behavior... |
| All times are GMT +1. The time now is 03:27 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.