-1

In my program i create a object for every connection and i want to retain those objects even after refreshing the webpage.how can achieve that?

<script>
var ip=prompt("Enter address of printer:", "12.222.170.99");
conn =new connection();
localStorage.setItem(ip,JSON.stringify(conn));
 var a=localStorage.getItem(ip);
 var b=JSON.parse(a);
b.fun();
function connection()
{
this.fun=function() 
{
alert("hi");`enter code here`
}
}

</script>
user4501328
  • 107
  • 2
  • 4

1 Answers1

0

If you are ok with using a jQuery plugin then you can use

$.cookie("cookie_name", JSON.stringify(json_object))

as shown in the following:

jquery save json data object in cookie

This uses the jQueryCookie plugin.

An Alternative is to use localStorage

// Store the object after you stringify it
localStorage.setItem('name', JSON.stringify(json_object));

// Get the object from storage and parse it back to json
var retrievedObject = JSON.parse(localStorage.getItem('name'));
Community
  • 1
  • 1
M. Laing
  • 1,607
  • 11
  • 25