0

The parsed anchor elements can be saved in localstorage by two steps.
step1
To open https://www.yahoo.com/ in chrome
step2
To run the code in inspect--console.

var as=document.getElementsByTagName('a');
var result='';
for(var i=0;i<as.length;i++){
    result +=as[i]+"  ";
 }
localStorage.setItem("parse_anchor", result);

enter image description here

To open localstorage, key is parse_anchor and value is result(a long string) can be seen.

enter image description here

Now to write the following code and save it as parse_anchor.html.

<html>
<script type="text/javascript">
window.location.href="https://www.yahoo.com/";
var as=document.getElementsByTagName('a');
var result='';
for(var i=0;i<as.length;i++){
     result +=as[i]+"  ";
 }
localStorage.setItem("parse_anchor", result);
</script>
</html>

No key-value pair can be seen in chrome's localstorage when to open the file parse_anchor.html with chrome.

showkey
  • 482
  • 42
  • 140
  • 295
  • Actually your key is `parse_anchor` and `result` is your value; if you want to store object in localStorage you might want to checkout this thread: http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage – t3__rry Oct 04 '16 at 11:49

1 Answers1

0

It looks like you're trying to save an array of DOM Object's into localStorage.

localStorage.setItem(key, value);

The key and value types are [DOMString][1] , but you're trying to save an object | array.

I don't know what you're trying to store on localStorage, but this is a snippet that actually works.

EDIT :

window.onload = function(){
    var as=document.getElementsByTagName('a');
    var result='';
    for(var i=0;i<as.length;i++){
         result +=as[i]+"  ";
     }
    localStorage.setItem("parse_anchor", JSON.stringify(result));
}
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
  • My problem is that when to save the code as parse_anchor.html,and to open the file `parse_anchor.html` ,no parsed anchor elements saved in localstorage. – showkey Oct 04 '16 at 12:08
  • `JSON.stringify(result));` doesn't matter. – showkey Oct 04 '16 at 12:10
  • `localStorage.setItem("parse_anchor", result);` has such the same effect as `localStorage.setItem("parse_anchor", JSON.stringify(result));`. – showkey Oct 04 '16 at 12:11
  • @it_is_a_literature missed that out completelly. Well the easiest way in web API without jQuery and stuff is to use the onload event of window. – Rosmarine Popcorn Oct 04 '16 at 12:15