Change the name of a local-storage collection - javascript

As per my new requirement I have to change the name of the local-storage collection name, which is by default set to its domain name. Is this possible? I have already searched it and didn't get anything. It is just to confirm that is this possible.
Suppose I have a domain named : http://www.example.com
then the local-storage will be created as http://www.example.com and its key value pair.
I want to change this from "http://www.example.com" to "http://www.example.com_local".
Please confirm me if it is possible anyhow. I am building an Umbraco website using MVC 4.
Thanks in advance.

If it's not the key you want to change, but the value, then remember that localStorage operates on strings and only persists the value when you call setItem.
Keeping a reference to the original value you are setting, and changing that will not change anything in the localStorage. When dealing with the localStorage HTML5 API, then you need to re-set the value when you change it. You also need to re-set the value (and remove the old) if you change the key.
If you are using some helper API/library, then anything(almost) could be wrapped in it, making it impossible for me to help unless i know what library that is.

Related

Generate a unique id in react that persist

I need to keep a unique identifier for each client using my react app.
doing this will regenerate a random string (what I want) but does this on each refresh which is not what I want
const [id] = useState(Math.random().toString(36).substr(2, 8));
I've found uniqueId() form lodash but I'm afraid the id's won't be unique across multiple clients as it only give a unique Id and increment it at every call (1, 2, 3...)
const [id] = useState(_uniqueId());
Is there some kind of _uniqueId that generates a random string and also persist through page refresh?
I don't think there is a built-in or out-of-the-box solution that generates unique id in react that persist automatically. You have two problems to solve.
How to generate unique id. Which was already solved by using the uuid.
And how to persist it.
There are plenty of storage you can use depend on your need. Here's few of them where you can persist your data assuming you want it to be stored in client side.
LocalStorage
SessionStorage
Cookie
IndexedDB API
FileSystem
Again, it depends on your use case. So, carefully check them out which one fits on your requirement.
Another way to generate a temporary ID that would be the same for the same client, without storing it is to use browser fingerprinting.
For example, you can take user-agent, client timezone, and screen resolution, apply some hash function to them and call it an ID.
There are more advanced ways of fingerprinting that would result in less chance of two different users having the same ID, but it'll never be a 0% chance.
You also might want to use some libraries, such as https://github.com/fingerprintjs/fingerprintjs for this.

Not allow localstorage to be changed by user

I use localstorage to store things like highscore in this game: http://wacky2048.ga/
If you inspect element and on the top navigation bar (where you see Elements ... Performance), then click the >> button, click Application, you can see all the localstorage items, and if you double click, you can change it. You may need to make a move and refresh.
A lot of people know this trick so the highscore becomes meaningless.
Is there any way to stop this? I store integers and JSON stringified things (in case you want to suggest a encoding method).
The better solution would be store the data in the server. But if you really want to use localstorage consider storing the JSON as a jwt token and encrypt it using a private key which user doesn't have access.
Also when your app access that data in the localstorage always check for validity. If the token is invalid, what you can do is re fetch the information from the server.
Like i said before this is more of a dumb approach. Storing data in the server would be a better solution.
Edit: To hide the private key you could use environment variables like NODE_ENV (this depends on the framework you are using)

Find the last visited URL in javascript with history

I know about document.referer and this is not what I'm looking for.
It could probably be a solution but I want to retrieve it in local without using server.
Steps :
I'm pushing a new state in the current url of my page with history.pushState('','','#test')
I'm going to the next page.
And now I want to retrieve the URL but history.previous has been erased since Gecko 26.
Is there an other way of getting this value except from using cookies or sessionStorage?
One way of doing it would be to put the value in a parameter of the next page address, like so:
http://server.com/next-page-address/whatever.html?prevData=RequiredStateData
Note that using this approach, you might be exposed to users changing RequiredStateData in a malicious way. You can protect yourself using encryption or some checksum field.
So my problem was that there is no option for that purpose in local without a server environment.
If you find this question it's that you're probably in the same problem as me so the only option I found was to use.
sessionStorage.setItem('foo',val) and retrieve it with sessionStorage.getItem('foo').

Modify a field within a cookie

Is it possible to edit a field inside a cookie using javascript? The cookie looks like this
cookie_session=[{"id":"1526","username":"test","email":"test#test.com"}]
For example, is it possible to edit the value of the field username?
I'm trying to use
document.cookie="Field=myValue"
But when i use it, it sets the whole value of the cookie to myValue instead of a certain field within it.
Also, would it be possible to parse the value of username to use it later for a POST request?
A cookie is simply a string. You are storing javascript objects inside of a cookie. In order to modify a single part of the object, you will need to decode the JSON, edit the property, and re-encode the object back to a string to store it in the cookie.
btw - cookies were not meant to store javascript objects. If you don't need the data on the server, then you are better off using local/session storage. There are jQuery plugins which allow those mechanisms to work in a cross-browser friendly fashion.
EDIT: an example can be found here: Pure Javascript - store object in cookie

Using variables after refreshing the page

I was wondering if it is possible to keep some data even after refreshing the page. For example, I have values in my array and I don't want to lose them after refreshing the page.
Use Cookies in javascript or the HTML5 localStorage,sessionStorage variable .
To use HTML5 LocalStorage, simply use it like variable preceded by localStorage.
localStorage.variable=['wrg','wg','wg'];
To use HTML5 sessionStorage, simply use it like variable preceded by sessionStorage.
sessionStorage.variable=['wrg','wg','wg'];
For more Info See Here
I have used jStorage in the past for storing data, its uses HTML 5 local storage where available and falls back to other methods when needed.
$.jStorage.set(key, value, options)
value = $.jStorage.get(key)
value = $.jStorage.get(key, "default value")
There is a trick w/ window.name in some browsers which allows to keep data even after refresh. Window name can store a json of your array.
window.name = "[JSON]";
BTW, Dojo implemented wrapper around window.name
Dojo WindowName
Set a cookie with your values and request the cookie.
One way is to create Cookie and store in it $.cookie("SomeKey", 1);

Categories