sessionStorage isn't working as expected - javascript

Here is my code:
sessionStorage.loggedIn = true;
if (sessionStorage.loggedIn) {
alert('true');
}
else {
alert('false');
}
Simple enough. There must be some small thing I'm not understanding about how JavaScript is evaluating these expressions. When I put sessionStorage.loggedIn = false, the "false" alert shows correctly. However, when I change sessionStorage.loggedIn to true, the "false" alert still pops, even after clearing the session. What am I not getting right with this expression? It seems so simple, maybe I just need another pair of eyes on it.

Try to change your code to
sessionStorage.setItem('loggedIn',JSON.stringify(true));
if (JSON.parse(sessionStorage.getItem('loggedIn'))) {
alert('true');
}
else {
alert('false');
}
and it should work consistently across all major browsers.
The interface with the setItem/getItem methods is how the spec is written, so going that way is safer than using the shortcut of assigning properties. Also, sessionStorage, like localStorage is a textbased storage mechanism, and not meant for storing objects, so you need to wrap calls with JSON.parse and JSON.stringify to get the expected results across the board.
Be aware that JSON.parse doesn't always play nice with undefined/null values, so it might be wise to do some type checking first.
You can read the spec for the storage interface here

Keys and Values in a WebStorage object (sessionStorage) must be strings. If they are not strings they "should" be converted to strings in the browser's implementation when you assign to sessionStorage. If you evaluate against "true" or convert to boolean it will work fine.
https://code.google.com/p/sessionstorage/
http://www.w3schools.com/html/html5_webstorage.asp

Related

Boolean to string wherever present in code

I have an app platform which is developed on "Edge". And my app is built on eclipse. In javascript files of my code many times i have declared my variables with boolean. But to run app on platform i have to convert these boolean values to string like "true". Then only i can set it true. In a big code it is not good to convert boolean to string everywhere. So is it possible that wherever i am having boolean value it can detect and convert to string so that the app platform developed on Edge can understand it ?
Why would you try to print out boolean values in the first place? Check to see if value is boolean where you are alert(value);
For example,
var value = true; // Somewhere in code
if (value == true) { // Just check if value is true/false
alert("true");
} else {
alert("false");
}
Your approach is unnecessary and you are making the problem more complex then needs to be. There is no way to check if a value is boolean while the code is running, so you are going to have to change it everywhere you want alert("true"); Sadly, you can't hack this with a different approach.

Javascript: localStorage paralyzes if-statements?

Made a boolean variable "disableReplaceDate", used in if-statements. Have to save the variable somehow because the page reloads and I need to use it to "check a status" sort of, so it doesn't return to default every time the page reloads.
Then, to save myself a lot of time and trouble, because I can't send them in the URL as parameters(it's occupied already by a complicated java file which manipulates it all and I'm no good at java at all), I decided to use window.localStorage setItem, getItem and removeItem etc., so basically
var disableReplaceDate = false;
window.localStorage.setItem("dRD", false);
disableReplaceDate = window.localStorage.getItem("dRD");
But now the if-statement, which looked as follows:
if(disableReplaceDate == true){/*do some stuff*/}
didn't work anymore! But then when I changed it to
if(disableReplaceDate){/*do some stuff*/}
It starts working suddenly.
And if that wasn't uncanny enough, it won't do this for all if-statements using the variable, I tried changing them to (!disableReplaceDate) and such, but it doesn't make them work.
Why is this? And how do I solve it?
localStorage only stores strings. And "false" evaluates as true in a context where a boolean is needed (for example a if statement).
You should change
disableReplaceDate = window.localStorage.getItem("dRD");
to
disableReplaceDate = window.localStorage.getItem("dRD") === "true";
I think because the type of your variable is "string" see this
typeof(disableReplaceDate)
The output will be string
I think you need to test this way if(disableReplaceDate === "false")

How to compare two noderefs in alfresco javascript

I am have two noderefs in alfresco javascript file which i am trying to compare as below.
if(personRef == userAsscNodeRef){
do something
}else{
do something else
}
It seems to be syntactically correct but always going to else part. I tried with strict equal i.e. === as well as adding .toString() to both noderefs but still same result.
How can it be possible?
Regards.
Assuming this is repo tier Javascript, you are really dealing with Java NodeRef objects and == behaves like it does in Java and compares identity. You want to compare equivalence, so use personRef.equals(userAssocNodeRef). Yes, pretty unexpected behavior. Beware of Strings and Date objects as well.
Use String():
if(String(personRef) == String(userAsscNodeRef)){
do something
}else{
do something else
}

Compare strings containing font icons

Note to people who pretend to be Moderators:
Do not dislike if you could not understand this question. It has enough information enough details and very useful to people who use font-icons in JS
I am trying to compare two strings that contain font-icons, but it is failed. I have tried comparing like below
a === b
a == b // Though Browser will do my conversion job
btoa(a) == btoa(b) // Browser scolded me.
Are there any alternative ways?
Fiddle
In your fiddle, element.innerText is returning undefined, hence it is failing.
Use element.textContent instead. Also, since your div contains , you'll need to take some extra precaution in some browsers since it might try and convert it into an object of some sort. (Currently happening on my browser)

Why doesn't IE7 think these two strings are equal?

I have an event handler that will remove an element from a list of the corresponding checkbox is unchecked. In the handler for the click event for the checkbox, first I copy the value of the label for that checkbox:
var label = $(this).next().html();
Then, I iterate over the list items and compare each of them to that label:
$("#sortable li").each(function() {
if ($(this).html() === label) {
$(this).remove();
}
});
In Internet Explorer 8 and in Firefox, this works exactly as I'd expect. In Internet Explorer 7, it does not. The condition in the "if" statement is never true. What am I missing here?
By request, the strings being compared are, "Charge" and "Charge" in one case.
Try alert(label) and alert($(this).html()) to see exactly what's being compared.
I'm not seeing any reason to use strict comparison (===), and you could conceivably benefit from using ordinary comparison (==).
As a method of doing what you're saying you want to do, this all seems pretty crazy. I'd always recommend identifying corresponding DOM elements by something more designed-for-the-purpose than their HTML contents, such as an ID.
Why don't you use the Script Debugger and see exactly why that comparison is false? Just set a breakpoint in the if and watch the variables.
The IE8 internal script debugger will help you, just switch to compatibility mode to run the script in the IE7 javascript runtime
Have you tried debugging it? Use alert to see what the two values hold, check that the function is actually called in IE7 and check that they have the same type.
Not an answer to your direct question, but here's another method that uses jQuery's data method to set and read a flag directly on the element:
$(this).next().data("toRemove", true);
//...
$("#sortable li").each(function() {
if ($(this).data("toRemove")) {
$(this).remove();
}
});
Of course, if you were manipulating label at any point then this wouldn't be what you want. I just mention this because .data() is a very useful jQuery feature that I haven't heard much about. I didn't even know about it until a week ago.

Categories

Resources