Is it safe to store JavaScript variables in the URL's hash for web application state restoration through bookmarks?
I've been considering using JSON serialization as a method. Storing variables like this:
var params = { var1: window.val1, var2: window.val2 }
window.location.hash = JSON.stringify(params)
And retrieving them like this:
var paramStr = window.location.hash.substring(1) // substring removes the initial "#"
var params = JSON.parse(paramStr)
window.var1 = params.var1
window.var2 = params.var2
While this approach seems simple and concise compared to other suggestions, I'm concerned about its security implications. Could malicious users exploit this by injecting harmful code into the URL?
As a novice in web programming, I'm unsure of the risks involved. Is storing variables in window.location.hash
a safe practice? What are the potential dangers associated with it?