[removed] Store function in localstorage

后端 未结 2 1941
梦毁少年i
梦毁少年i 2020-12-30 13:45

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

$(\'#pass_to_score\').on(\'click\',function(){

var compressed = function(){
         


        
2条回答
  •  不知归路
    2020-12-30 14:38

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

    Store it:

    var myFunc = function (){
      alert('Hello world!');
    };
    
    // Store it as a String
    localStorage.setItem('compressedFunc', myFunc.toString());
    

    Later, retrieve it:

    var compressedFunc = localStorage.getItem('compressedFunc');
    
    // Convert the String back to a function
    var myFunc = eval('(' + compressedFunc + ')');
    
    // Use it
    myFunc();
    

提交回复
热议问题