Modify a field within a cookie - javascript

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

Related

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)

Change the name of a local-storage collection

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.

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);

store user input without mysql or php

I have a simple html application which displays words on a click of a next button. It fetches the words from a javascript object literal file. I want to mark some of the words as easy and some as difficult. How do I save this data from browser without using a mysql database?
can I edit the javascript object file directly from bowser?
If you want to take user input and store it permanently on your site, you'll have to employ some sort of server-side scripting. This doesn't have to be PHP, but it's probably the simplest way to do it. You can't use client-side javascript to write to a remote file directly.
If I understand correctly, you have JS object/array with words, you're modifying it and want to store it modified version permanently.
If so, then you can use "HTML5" localStorage.
This storage is per-browser. If you want to have single version shared between many users/browsers, then you will need some server-side support.
To save it on the client-side you could use cookies or local storage on supported browsers but this may not be the best approach if there are many words to keep track of.

Using jQuery to set/retrieve multiple key/value pairs in one cookie using an array

My goal here is to cut down the number of cookies I'm using to store things like persistent states into a single cookie by setting and retrieving multiple key/value pairs within a single cookie using an array.
I'm mostly interested in knowing if someone else has already done this and has written a plugin for it, as I would think this has been done before.
You could serialize the object into a JSON string. That would make it super simple to re-load the object. See question 191881 for information on how to serialize an object to JSON in JavaScript.

Categories