Oussama Bouthouri
Oussama Bouthouri

Reputation: 655

Keep the changes of a web page after refresh

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 ?

Upvotes: 2

Views: 10354

Answers (3)

Sphvn
Sphvn

Reputation: 5343

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.

Upvotes: 2

DadViegas
DadViegas

Reputation: 2291

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 computer. Normally JavaScript is not allowed to read or write files on the client-computer (an important security feature), but it can generate data strings that the Web browser can store in a special file commonly 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.

Upvotes: 1

Rafael Emshoff
Rafael Emshoff

Reputation: 2729

Microsoft has a good guide on state management for web applications. Check it out and you'll see all the options that would come 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.

Upvotes: 0

Related Questions