Raffy T Lawrence Raffy T Lawrence - 1 year ago 97
Javascript Question

Javascript: Store function in localstorage

Is this possible to insert to localstorage or is there other way to store this?


var compressed = function(){
$('#title_panel').html('Edit criteria scoring');
localStorage.setItem('compressed', compressed());
//i also try set as JSON.stringify but its undefined


Answer Source

I don't know why you'd want that, I would not recommend it, but you can do it using toString.

Store it:

var compressed = function (){
  alert('Hello world!');

// Store it as a String
localStorage.setItem('compressed', compressed.toString());

Later, retrieve it:

var compressedString = localStorage.getItem('compressed');

// Convert the String back to a function
var compressed = eval(compressedString);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download