OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

export Data in localStorage for later re-import

  • Thread starter Thread starter Zim84
  • Start date Start date
Z

Zim84

Guest
I want to export a few items from my localStorage to save it externally but in a format so that I can import it again later.

My attempt was to write executable code that can be pasted later in a textarea. Then the value of that textare will simply be eval()ed.

Problem: The data stored in localStorage were stored as

Code:
var data = [];
data.push('sampledata');
data.push({sample: 'object'});
localStorage.setItem('varname',data);

So it contains various chars I don't like, like ', " etc

My (not working) solution so far was:

Code:
var container = $('#localDataContainer');
container.append('localStorage.setItem("cockpitLastVisited","' + localStorage.getItem("cockpitLastVisited") + '");<br/>');
container.append('localStorage.setItem("cockpit_services","' + localStorage.getItem("cockpit_services") + '");<br/>');
container.append('localStorage.setItem("cockpit_users","' + localStorage.getItem("cockpit_users") + '");');

If my attempt seems to be OK, what is the best way to create code that can then be executed the way it is?

<p>I want to export a few items from my localStorage to save it externally but in a format so that I can import it again later.</p>

<p>My attempt was to write executable code that can be pasted later in a textarea. Then the value of that textare will simply be eval()ed.</p>

<p>Problem: The data stored in localStorage were stored as</p>

<pre class="lang-js prettyprint-override"><code>var data = [];
data.push('sampledata');
data.push({sample: 'object'});
localStorage.setItem('varname',data);
</code></pre>

<p>So it contains various chars I don't like, like ', " etc</p>

<p>My (not working) solution so far was:</p>

<pre class="lang-js prettyprint-override"><code>var container = $('#localDataContainer');
container.append('localStorage.setItem("cockpitLastVisited","' + localStorage.getItem("cockpitLastVisited") + '");<br/>');
container.append('localStorage.setItem("cockpit_services","' + localStorage.getItem("cockpit_services") + '");<br/>');
container.append('localStorage.setItem("cockpit_users","' + localStorage.getItem("cockpit_users") + '");');
</code></pre>

<p>If my attempt seems to be OK, what is the best way to create code that can then be executed the way it is?</p>
 

Latest posts

Top