سؤال

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?

هل كانت مفيدة؟

المحلول

There are different libraries available:

Cookies:

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

Local Storage:

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

نصائح أخرى

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.

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");

Localstorage API can be useful for you.

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top