Take the 2-minute tour ×

I have a webpage game, it's multiplayer so the page has to refresh every few seconds so the player can keep track and know when it is their turn...

It refreshes using the code:

<META HTTP-EQUIV="Refresh" CONTENT="3; URL=game.php">

Also the game page has a 'chatroom' in it, which basically allows the players to 'chat' to each other, the chatroom consists of a div (which holds text from database) and a HTML form textbox/textfield and a HTML form submit button.

But because the page refreshes if the user is in the middle of typing something like 'Hello players' and gets to 'Hel' before the page automatticly refreshes the textfield/textbox loses the value ('Hel')

How can I easily put the 'Hel' value back into textbox/textfield as if it never was gone so the user can continue typing his message?

Or is there a way to never lose the value?

share|improve this question
Is Ajax an option? If so, don't refresh the page at all and just make an Ajax request back to the server to retrieve any new data. If you want to go all out; look in to Comet en.wikipedia.org/wiki/Comet_(programming) to push updates to the client. –  Chris Baxter May 14 '11 at 15:11
I use AJAX already to get the chat, using it for the whole game would be annoying and unnecessary. I just need the textbox to hold its value on a refresh. –  Steven Tilling May 14 '11 at 15:12
As long as you are refreshing the page, you won't be able to provide a seemless user experience. The user may still be typing when the page refreshes, and not all user input will be captured. As for alternative options... store the value in a cookie on the textbox propertychange and restore the value on page load; hackish but the best you will probably get: w3schools.com/JS/js_cookies.asp –  Chris Baxter May 14 '11 at 15:14
add comment

2 Answers

up vote 1 down vote accepted

Have a look at http://www.w3schools.com/JS/js_cookies.asp, it shows you how to set and get cookies in JS. You could set the textbox value on page load from the cookie and set the cookie value whenever the contents of the textbox changes. I do have to agree that this seems an odd and awkward way of doing this but I have not seen the full thing so...

I can probably give you example code for this but I have not done any JS for a few months now and am currently in VB.Net mode. If someone else could provide a basic example of this I would be really grateful.

share|improve this answer
add comment

Just use javascript and update all you need with AJAX.

share|improve this answer
The chat updates with AJAX thats not a problem, the only problem is the textfield/textbox value is lost (What the user was typing before refresh) I was hoping you could give me the javascript not just tell me it exists? :/ –  Steven Tilling May 14 '11 at 15:11
add comment

Your Answer


