Strange Issue with javascript setting the local storage - javascript

I want to set the local storage with unique key with "DONE" value
for example :
PdfSeen as key DONE as value
VideoSeen as key DONE as value
both
localStorage.setItem("PdfSeen" , "DONE");
localStorage.setItem("VideoSeen" , "DONE");
set a new entry in the local storage with
key DONE and value undefined
my expected result is
PdfSeen as key DONE as value
VideoSeen as key DONE as value
please check this below image to understand the issue well
the below code leads to the above entry in the localstorage

I think I found your issue. You have to pull down your bar on local storage. See the following images:
Now use the cursor to drag the line some pixels down and you will see the real keys:

undefined in the console only means, that there was no return value.
localStorage.setItem("PdfSeen", "DONE"); // will log `undefined`
console.log(localStorage.getItem("PdfSeen")); // will log the value.
also see Using the Web Storage API.

Related

How to store fetched data from API to Global Variable

As seen in this picture, I have initialized variable madhu globally and assigning the fetched data to this variable inside .then method but it's showing me undefined.
How can I store this value globally?
picture
You need to understand the difference between synchronous and asynchronous code. The value is setting correctly as in line 160 after response is received but line 167 runs before 160 and at that time the value was not set.
You can do it in different options.
The simplest is using localStorage already in the HTML5, you could save the data in the browser
localStorage.setItem('response', JSON.stringify(data));
myData = JSON.parse(localStorage.getItem('response'));
sorry my english is not very good, I hope I have explained myself well

Creating a 'truly global' integer variable using Web API Storage

I have two html documents, and I want them to share a variable and its value.
In lieu of 'truly global' variables in JavaScript, I've tried employing Web API Storage:
var number = parseInt(localStorage.setItem('num',0));
Storage is always a string, so I try parsing into an integer seemingly incorrectly (rather than showing up as 0 in my program, it shows up as 'NaN'). I can only assume that my syntax's wrong?
I want this variable to be increaseable (+1 every time the user clicks on something) but have not yet figured this out. I then want to retrieve its value in the second html document. But due to the initial misstep in attempting to parse the storage value I cannot yet attempt these methods.
It doesn't work like that because localStorage.setItem returns undefined. The way to do it would be:
// define variable
var num = 0;
// set to storage
localStorage.setItem('num', num);
// get from storage
num = parseInt(localStorage.getItem('num'));
// write-back incremented
localStorage.setItem('num', num + 1);
localStorage.setItem stores the value but doesn't return anything - or in other words returns undefined. Trying to parse undefined as a number returns NaN.
Note that localStorage does not work across files when viewing files in your browser from file://, you'll need to run a minimal server to test your code.

Access $key from object returned from firebase

I have an object downloaded from firebase which is of type Bug (Custom type). However, when I tried to console log the output with the following code
console.log('bug',this.bug);
I get the following result. It is what I expected where I can go on and do things like
console.log('company', this.bug.companyName)
However, I want to get that key value as well. Howe can I do that? I tried to do
console.log('company', this.bug.key)
console.log('company', this.bug.$key)
and both of them does not work
try to run
console.log('company', this.bug["$key"])
remember property lookup on objects is either
obj.someKey // or
obj["someKey"]

Setting Object to Chrome Storage

So I'm trying to save an object via the chrome.storage API. The relevant code is:
var images = null;
var storage = chrome.storage.sync;
var key = "key";
images = getImages(source);
alert(images.length); // returns 4
storage.set({key: images});
storage.get("key", function(result) {
alert(result.length); // returns undefined
});
I'm tested that immediately after the getImages() function, images is a wrapped set JQuery object with a length of 4. However, when I try to access images.length via the storage.get callback, the result is undefined.
Could someone help identify the error in how I am storing and/or retrieving this JQuery object?
Update 1:
Thank you all for your help. As clarification for the use case, I am using chrome.storage instead of localStorage because I plan to pass extension info to another script.
Fortunately, TranQ/Xan's solution has enabled me to access the array via the storage.get call.
I'm still experiencing issues working with the wrapped set JQuery object stored in the array but I'll post a separate question since the current solution encapsulates broader use cases.
TranQ's comment is on point.
Presumably, images is an array. You store that array under the "key" key.
When you execute the get() function, it returns an object populated with all key-value pairs you asked, even if you only ask for one key.
So, result is an object {key : [/* something */]}. Objects do not have a length property, and you get undefined
You need to use result.key (or result["key"]) to access your array.

Shared objects between windows Javascript

I have an application which open several windows (with javascript) in the same domain.
I would like to share some javascript objects between these windows (an object which contains some configurations for example).
Is it possible to do this in javascript and how to do this ?
Thanks.
There are 2 possibilities: local storage and session storage
The session storage stores value for duration of the session, the value gets deleted when browser is closed and re-opened.
// Store value
sessionStorage.setItem('key', 'value');
//or
sessionStorage['key'] = value;
// Retrieve value
alert(sessionStorage.getItem('key'));
The local storage stores value beyond the duration of the session, the value can be retrievedeven after closing and re-opening the browser.
// Store value
localStorage.setItem('key', 'value');
//or
localStorage['key'] = value;
// Retrieve value
alert(localStorage.getItem('key'));
Use localStorage along with JSON to store your objects as strings:
Setting:
window.localStorage.setItem('yourKey', JSON.stringify(yourObject));
Getting:
var yourObject = JSON.parse(window.localStorage.getItem('yourKey'));
the localStorage data will be shared across all of your pages as long as they exist in the same domain.
If you used window.open() to create the second window, window.opener in the second window might give you access to the first window. See MDN.

Categories

Resources