Reputation: 3613
I'm fairly new to HTML, PHP, CSS and Javascript. I do have a bit of a background in C++ if that means anything. To practise my website programming, I decided to make a website with a simple sort of chat room. Basically, users log into the site, and when they enter a message into the 'chat room' (by typing in the text field and clicking the submit button), the message is appeneded to ChatData.txt. Then ChatData.txt is read and displayed in an iframe. The iframe only displays the chat data and is refreshed every 5 seconds. This is all done using PHP and HTML.
What I would like to know is, is it possible to, instead of refreshing the iframe every 5 seconds, force all the clients to refresh the iframe only when the submit button is clicked?
I was thinking that somehow there'd be a way to send a refresh to all the browsers using PHP/Javascript.
Thanks.
Upvotes: 0
Views: 306
Reputation: 5731
You can remove the iframe node and rebuild it via javascript dynamically.
Below is a basic method in how to do it via jQuery. You'll need a wrapper (<div id="reload"></div>
) around your iFrame with an ID called "reload".
$(document).ready(function() {
setInterval(function() {
$("#reload").html('<iframe src="/path/to/script.php"></iframe>');
},5000);
});
This may achieve your goal but is not ideal since it can be a burden on your browser. Ideally async calls to your script and creating HTML nodes by javacript would be the better route. However this will require a little bit more researching/learning on your end in order to build it properly. (you dont get the flashing iframe screen due to reload)
Upvotes: 1
Reputation: 1030
What a refresh technically does is force the client to manually ask for new data - even if none exists. What you're trying to do is, however, to have your server/backend push new changes as they become available.
There are a number of methods to perform this. Here's a good place to start: How do I implement basic "Long Polling"?
Upvotes: 1