tags:

views:

387

answers:

3

Hi,

I have a javascript function that writes the value of input box to a cookie, then it tells the page to refresh. The page must be refreshed to the server side can reconstruct the page based on the values of the cookies. Output the correct data on the page.

However I do not want the user to lose the value they type on the input box unless they erase it.

what can i do?

A: 

If the user is refreshing the page manually, check out window.onbeforeunload.

If the user is posting the form, then spit out the value server side or use JavaScript to get the cookie value.

Detect
i am confused, do you put that in the input box tag?
A: 

Don't refresh. Redirect the user back to the same url, but with an extra querystring parameter. Pick this up in JS and populate the textbox.

mcintyre321
A: 

If you're trying to set the value of the text input after the refresh, use this Javascript:

var inputval = getCookie('nameofcookie');

if(inputval != NULL && inputval != "") {

     document.getElementById('idofinput').value = inputval;

}

Replace "nameofcookie" with the name of the cookie that the input value is stored in, and replace "idofinput" with the ID of the text input field.

gclaghorn
when and where would that code snippet be executed, inside the function that refreshes the page or outside?
Outside the function, when the page is loaded.
gclaghorn