0

say someone using my site is navigating page to page or doing some action on the website. is there a way to store string on the client side, the urls that he visited for example? Is it also possible to overrwrite a string that was written? Ideally something that has key, value storage, and let's one go back and edit a value by it's key.

It should work on all browsers, is there such library?

5 Answers 5

2

There are different libraries available:

Cookies:

https://github.com/carhartl/jquery-cookie

Local Storage:

https://github.com/medialize/jQuery-store (recommended)

Sign up to request clarification or add additional context in comments.

Comments

1

Seems to me, your problem with using localStorage is that you will be constrained to saving data for only the page you are on, and it won't expire. It also won't work on browsers that don't support HTML5. Perhaps expiration is not an issue for you, but if you use document.cookie instead, you can set an expires date for the cookie and also set a path so that the same cookie will be available to all of the pages on your site. There's a tutorial for document.cookie here. There are also tons of libraries for working with cookies. jQuery and Angular each have one, and I'm sure there are others.

3 Comments

so if a user navigates away from a page, but still on the same domain, does the cookie still persist? what if I want to clear the cookie when they navigate away from the page or close the browser window of my site?
Question one: yes, the cookie persists. That is what you looked for. Question two: where is the benefit of doing so? Imagine the situation that the user comes back to your site.
According to Wikipedia, the spec behavior is for cookies to be removed automatically when the browser closes. Of course, not all browsers are guaranteed to FOLLOW the spec, but I have to agree with @PaulFacklam in that I'm not sure you'd necessarily want to do that with something like a history.
0

This is easy to do, using HTML5's localstorage.

Here is the code to store a string:

localStorage.setItem("localStorageID", "This is a string I stored");

Comments

0

Localstorage API can be useful for you.

More information about this http://www.w3schools.com/html/html5_webstorage.asp

Comments

0

If you are using HTML5 you can store in Local storage. But it wont work in older version of browsers.

You can use cookie to store data. But every time user request a page he will send the cookie information to the server. So you should not store unnecessary data in cookie.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.