0

I am writing a webapp that utilizes localstorage. I have it setup now to save to the local storage with the following format:

Key: Four digit number Value: My data

I need to take all the separate data from the localStorage, and output it to a single file with the following format:

XXXX <- Four digit key
Data
-linebreak-

How would I go about doing this? Also, is it possible to somehow take all this information, and send it via email. Or some way to get it out of localstorage and to clipboard so the user can copy it into their email.

Thanks ahead.

das_boot
  • 113
  • 2
  • 9

2 Answers2

1
var output = "";
for(var key in localStorage) { 
    output += key+"\n";
    output += localStorage[key]+"\n";
    output += "\n";
}

// output contains combined string

As for the email part you could try using a mailto: like this,

<a href='mailto:user@domain?subject=[subject here]&body=[email body here]'></a>

This could be combined into a function like this:

function sendLocalStorageByEmail(recipient) {
    // create localstorage string
    var output = "";
    for(var key in localStorage) { 
        output += key+"\n";
        output += localStorage[key]+"\n";
        output += "\n";
    }

    // create temporary anchor to emulate mailto click in new tab
    var anchor = document.createElement("a");
    anchor.href = "mailto:"+recipient+"?subject=Local+Storage+Data&body="+encodeURIComponent(output);
    anchor.style.display = "none"; 
    anchor.setAttribute("target","_blank");
    anchor.appendChild(document.createTextNode(""));
    document.body.appendChild(anchor);

    if (anchor.click) {
        return anchor.click();
    }

    // some browsers (chromium/linux) have trouble with anchor.click
    var clickEv = document.createEvent("HTMLEvents");
    clickEv.initEvent("click", true, true);
    anchor.dispatchEvent(clickEv)
}

Usage:

<a href='javascript:sendLocalStorageByEmail(prompt("Please enter your e-mail address"))'>
   Send Email
</a>
lostsource
  • 21,070
  • 8
  • 66
  • 88
  • Never mind, it does work.I can't type. To output that combined string into an email how would I do that? – das_boot Dec 02 '12 at 20:09
  • check updated answer, I have included a function which takes care of the e-mail sending part. – lostsource Dec 02 '12 at 20:35
  • Holy jesus, you are amazing. Last question. In my variables, it has foreign language characters like å ä ö , for some reason it shows up in the email field as a � – das_boot Dec 02 '12 at 22:22
  • check updated answer, I changed the function which encodes the characters from `escape` to `encodeURIComponent` – lostsource Dec 02 '12 at 22:28
0

It is possible to integrate directly mail adress in the code, before mailto:

anchor.href = "mailto:*youradress@ttt.com*"+recipient+"? 
subject=Local+Storage+Data&body="+encodeURIComponent(output);
Dmytro Dadyka
  • 2,208
  • 5
  • 18
  • 31