I am working on a website that tracks which links the user has clicked. The data is stored as a string in localStorage
. I know that string data in localStorage
can be converted to JS objects by using JSON.parse
. The problem is that the stored url strings contain characters that aren't valid for JSON. I am getting the error:
1_0.html:1 Uncaught SyntaxError: Unexpected token h in JSON at position 0
at JSON.parse (<anonymous>)
at trackLink (tracking_functions.js:318)
at HTMLAnchorElement.onclick (1_0.html:46)
I tried cleaning the string before sending it through the JSON
parser using this suggestion but that didn't do the trick. And I am not sure if cleaning the string is even an option because it might make the url string unusable as a hyperlink.
Is there a way to get around this?