2

I am building a little HTML program (well, not little), but I have got options that I would like to permanently write to the client's machine without using cookies. reason being is cookies will expire, and have a storage limit of 4 KB. how would I do that without jquery.

user6234002
  • 2,415
  • 2
  • 10
  • 5
  • You will have to expound on the type of program you are writing. Since you are talking cookies, maybe it's an online program? Java, Java Applet, downloadable, Android in Java native, working online program from website, etc? If it's purely online, you can't. Well there is also something called a server-side cookie. That might help. – ejbytes Jun 09 '16 at 04:20
  • It is a HTML page that will turn into Android app. – user6234002 Jun 09 '16 at 04:21
  • sorry, that was kinda obvious. – user6234002 Jun 09 '16 at 04:21
  • You'd want to explain the details by editing your original post. I've created all kinds of apps and online programs using Java, Java Applets, Android apps not using Java and using Java. But I've never used a cookie unless it was a website I was creating. Well for one I wasn't selling the applications. I have used cookies, client side, and server side. Client side cookies can be deleted any time. Server side cookies cannot be deleted. You can't write to a machine from an HTML page though, it's not allowable by any means. It would have to be a hack and a virus if such a violation would occur. – ejbytes Jun 09 '16 at 04:31
  • 1
    This is probably more suitable for [so]. That being said, there’s also Local Storage. – Daniel B Jun 09 '16 at 06:08
  • thanks for the feedback and help – user6234002 Jun 10 '16 at 10:44

2 Answers2

0

Client side cookies can be deleted any time. Server side cookies cannot be deleted and the expiration date can be set. You can't write to a machine from an HTML page though, it's not allowable by any means. It would have to be a hack and a virus if such a violation would occur.

ejbytes
  • 183
  • 1
  • 9
0

You should use HTML5 Local Storage.

To set a item to the storage

localStorage.setItem("person", "jones");

To get the item

var val = localStorage.getItem("person");

To remove the item

localStorage.removeItem("person");

Depending of what browser you use, most have 10mb of storage. read here

The storage can be stored as long as the user or the application allows it. read here

The localStorage can only store values as strings. If you want to store objects like json

var data= { person: "jones" };
localStorage.setItem("jsonPerson", JSON.stringify(data));

To read the json

var data = JSON.parse(localStorage.getItem("jsonPerson"));
Community
  • 1
  • 1
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • so, can you call .setItem More than once? if yes, how would it output that? – user6234002 Jun 10 '16 at 12:04
  • @user6234002 Yes, you can call setItem as many as you like. If you set first localStorage.setItem("person", "jones"); and then localStorage.setItem("person", "andrea"); the output of localStorage.getItem("person"); will be andrea – Marcus Höglund Jun 10 '16 at 12:10
  • @user6234002 You can also set different objects to the localStorage like localStorage.setItem("person", "jones"); and localStorage.setItem("monster", "andrea"); – Marcus Höglund Jun 10 '16 at 12:11
  • @user6234002 updated my answer with json storage as well – Marcus Höglund Jun 10 '16 at 12:19
  • nice, thanks :-). but quick question, can you write more than one variable with a single command to storage? – user6234002 Jun 11 '16 at 08:34
  • You can only set one at a time to the storage. @user6234002 You can instead create a json obj and store multiple variablers in that. Please upvote if my answer has to any help:-) – Marcus Höglund Jun 11 '16 at 10:27
  • I have, but it says I need a total of 15 reputation. – user6234002 Jun 11 '16 at 23:42