jay
jay

Reputation: 338

Add localstorage link as a image src

Hello I have a local storage link that needs to be displayed as a img src. If the local storage link is google.com I want it to be displayed as a img src

code so far

<body>
  <div id="result">
  </div>
  <img src="" id="photo" /> //this is the img element
  <script>
    if (typeof(Storage) !== "undefined") {
      var name = localStorage.getItem('name');
      var dataImage = localStorage.getItem('imgData');
      document.getElementById("photo").innerHTML = localStorage.getItem("name"); //localstorage 
    } else {
      document.getElementById("photo").innerHTML = "Sorry, your browser does not support Web Storage...";
    }
  </script>
</body>

I know this question is confusing but this is the best I could describe.

Upvotes: 2

Views: 1208

Answers (1)

Matthew Moran
Matthew Moran

Reputation: 1497

You can set the image src attribute to the value you get from local storage. (Assuming that's a path to an image)

You would not want to set the innerHTML of an img tag. Instead, use another element to display text.

<div id="result"></div>
<img src="" id="photo" /> //this is the img element
<div id="msg"></div>
<script>
  if (typeof(Storage) !== "undefined") {
    var name = localStorage.getItem('name');
    var dataImage = localStorage.getItem('imgData');
    var imageName = localStorage.getItem("name");
    var imageEl = document.getElementById("photo");
    imageEl.src = imageName 
  } else {
    document.getElementById("msg").innerHTML = "Sorry, your browser does not support Web Storage...";
  }

</script>

Upvotes: 4

Related Questions