Jericho
Jericho

Reputation: 69

Saving HTML data Client-side

If I assign a text input tag to the id stored_data1 in the code below, would I be able to save my data client-side (such as saving it on a flash drive)?

HTML

Data that is retrieved will appear here
<div id="dataStore"></div>

JS

<script>
  if (typeof Storage !== 'undefined') {
    localStorage.setItem('stored_data1', 'Blue Box');
    document.getElementById('dataStore').innerHTML =
      localStorage.getItem('stored_data1');
  } else {
    // in case web storage is not supported
    document.getElementById('dataStore').innerHTML =
      'Web storage not supported.';
  }
</script>

The code above is a web storage API for HTML. I'm hoping to find a way to eventually fit it into some programs I made.

Upvotes: 2

Views: 226

Answers (2)

hossein vatankhah
hossein vatankhah

Reputation: 214

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<p>Data that is retrieved will appear here</p>
<div id="dataStore"></div>
</body>
    <script>
    if (typeof(Storage) !== "undefined") {
      window.localStorage.setItem("stored_data1", "Blue Box");
      document.getElementById("dataStore").innerHTML = window.localStorage.getItem("stored_data1");
    } else {
      document.getElementById("dataStore").innerHTML = "Web storage not supported.";
    }
</script>
</html> 

Upvotes: 1

BETOMBO
BETOMBO

Reputation: 194

You can download the information on the tag using the following code

//create a function to download text to textfile
function downloadText(text, filename){
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
}


//get Text Value then download 
var textValue = document.getElementById("test").innerText;
download(textValue, "store_data");

Upvotes: 1

Related Questions