Raffy T Lawrence Raffy T Lawrence - 2 months ago 13x
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



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