How to read local Storage JSON in angular index.html file and show in View page source as a script

634 Views Asked by At

I have JSON in my localStorage which I want to read in my angular index.html file and also I want to show this JSON when I see the View page source. Below code, I have tried. Note: When I am seeing View page source then only Plain HTML is showing noting dynamic from localStorage.

<html>
<body>
<script type="text/javascript">
    window.onload = function() {
      alert(JSON.stringify(JSON.parse(localStorage.getItem('profileJson'))));
      var data = JSON.stringify(JSON.parse(localStorage.getItem('profileJson')))
      var nameFromJson = data.default.provider.name
      document.getElementById("demo").innerHTML = nameFromJson;

    }
  </script>
  <p id="demo"></p>

  <app-root></app-root> 

</body>
</html>
1

There are 1 best solutions below

2
sno2 On

View page source only gets the raw HTML then displays it. Therefore, you wouldn't be able to access the local storage when you're getting that text out because it would require JavaScript to be run.