Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Uncaught SyntaxError: Unexpected string</code> is coming from <code>JSON.parse</code>, this is because it is no longer valid JSON (<a href="http://json.org/" rel="nofollow noreferrer">http://json.org/</a>)</p> <p>What you are doing is adding an object to an object, which turns out to be a string</p> <p><img src="https://i.stack.imgur.com/t0ZfE.png" alt="enter image description here"></p> <p>Then when you go to stringify it, the entire thing will be taken is just a string the first time and the second time. So it will go through fine, then the third time when it tries to parse it, the function will fail because you have added a JSON object with a string.</p> <p>If you are only storing JSON objects you can use jQuery's extend function (<a href="http://api.jquery.com/jQuery.extend/" rel="nofollow noreferrer">http://api.jquery.com/jQuery.extend/</a>)</p> <p>Or if you are storing more then just objects change it all to an array </p> <p>This should cover everything</p> <pre><code>function addtoStorage(key, data) { if (typeof(Storage) !== "undefined") { if (localStorage.getItem(key)) { console.log("Local Storage stuff" + localStorage.getItem(key)); var olddata = JSON.parse(localStorage.getItem(key)); var newdata = null; if(olddata instanceof Array){ olddata.push(data); newdata = olddata; }else if(data instanceof Array || !(data instanceof Object) || !(olddata instanceof Object)){ newdata = [olddata, data]; }else if(data instanceof Object &amp;&amp; olddata instanceof Object){ newdata = $.extend(olddata, data); } var dataJSON = JSON.stringify(newdata); localStorage.setItem(key, dataJSON); } else { var dataJSON = JSON.stringify(data); localStorage.setItem(key, dataJSON); } } else { console.log("You don't have storage capabilities. Sorry. Next time improve your browser."); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload