最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep the changes of a web page after refresh - Stack Overflow

programmeradmin1浏览0评论

I recently developed a script to highlight text in a web page based on document.execCommand() but the changes are gone if I refresh my web page.

How can I keep the change for every user ?

I recently developed a script to highlight text in a web page based on document.execCommand() but the changes are gone if I refresh my web page.

How can I keep the change for every user ?

Share Improve this question edited Sep 14, 2012 at 8:53 Alex K. 176k32 gold badges274 silver badges296 bronze badges asked Sep 14, 2012 at 8:51 Oussama BouthouriOussama Bouthouri 6553 gold badges8 silver badges23 bronze badges 3
  • Can you please explain what exactly you want to persist after a refresh? – Sphvn Commented Sep 14, 2012 at 8:56
  • i would like to keep the highlited text with the document.execCommand() function. i cant use server side so it will be client side. thank you again. – Oussama Bouthouri Commented Sep 14, 2012 at 21:51
  • Hope this discucssion help someone with the same question stackoverflow./questions/16206322/… – yu yang Jian Commented Jan 30, 2019 at 8:17
Add a ment  | 

3 Answers 3

Reset to default 2

As I am rather unsure what you actually want to persist I will give some generic information.

Some good reading at DiveIntoHtml5 on storage.

I would suggest taking a look at either sessionStorage or localStorage now while these are regarded generally as HTML5 the browser support is much greater.

You can see the support of keyValueStorage at CanIUse

You can store a key / value pair as follows:

localStorage.setItem("key", "value");

You can then retrieve the value as follows:

localStorage.getItem("key");

Remove:

localStorage.removeItem("key");

sessionStorage works the same as above but will only persist while the browser is open. It persists for the "session" of the browser. However localStorage will persist until it is removed by code or by clearing the browser.

There are two ways to save state.

One is to write client-side code that passes information back to the server for storage.

The other is to save what is called a cookie on the client puter. Normally JavaScript is not allowed to read or write files on the client-puter (an important security feature), but it can generate data strings that the Web browser can store in a special file monly referred to as a cookie jar. The cookie jar is a configuration file, a file that provides information on how to set up the browser.

Remember that no cookie can be larger than 4KB.

Microsoft has a good guide on state management for web applications. Check it out and you'll see all the options that would e in question for you. Then pick whatever seems best fit. Once you know what you want, you can search stack overflow for a concrete implementation of your problem. There's bound to already be an answer.

Edit: Table 5.5: "State Management Mechanisms for Web Applications" is the one you want to look at for an overview.

发布评论

评论列表(0)

  1. 暂无评论