开发者

can i make javascript persist after a refresh?

开发者 https://www.devze.com 2022-12-23 01:36 出处:网络
In my form i need to select an item (which is a div with text or an image) and i do set the id of a hidden input via jquery live click event.

In my form i need to select an item (which is a div with text or an image) and i do set the id of a hidden input via jquery live click event.

In the case the user has logged out or session has expired he must go back and refresh the page (aft开发者_如何学Goer logging in). After a refresh firefox keeps all input data (textarea, text, select, etc). BUT the value i set in the hidden input is reset back to 0 and the user must click the item again.

How can i make javascript remember after a page refresh? Everything else is remembered after a refresh, its natural for the user to expect this is too instead of being annoyed every time some error occurs with his entry.


You can store some information in a cookie, or in the window.location.hash key or in the window.name

All these values will be kept when you refresh.


Try to use type="text" and style="display:none" instead of type="hidden"


Sounds to me you can use cookies to store the selected value. Don't forget to clear once the form has been processed.

EDIT 1

If you have a situation when the user can submit it more time, you must prefix each cookie with an ID to denote the current form.
When the form is presented to the user first time, make a hidden field having the timestamp stored in it (This can be done on the server side). Then you can use this timestamp for the cookie prefix, so you will have cookies for the form that has been generated, and another timestamp for the 2nd form the user works with. You will have independent cookies for each form you open in your browser.
When you submit the page, the timestamp is sent to the server and you can setup cookies. If the user hits the browsers back button, it will go back to a form, where you already have the timestamp in your form's html source, so it will load the cookies for that form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号