Check if exist any Key localStorage Javascript - javascript

I need to check if there is any key in localStorage
This code did not work
if (localStorage.user === undefined) {
alert('There is no key!');
}

if (Object.keys(localStorage).length === 0) {
alert('There is no key!');
}

You need to use getItem
if (localStorage.getItem("user") === null) {
alert('Não há chave!');
}
EDIT
You can check if the localstorage is empty by
if (localStorage.length == 0)

Related

is this the right way to pull booleans from localstorage?

function loadChoice(key, varToStore) {
if (window.localStorage.getItem(key) === "true") {
varToStore = true;
}
else if (window.localStorage.getItem(key) === "false") {
varToStore = false;
}
else {
varToStore = window.localStorage.getItem(key);
}
}
I think I found a way to pull booleans (that got converted to strings before) from the web-browser localstorage.
can anyone confirm or have a better method ?

How to check whether session is null or not in javascript?

How to check whether session is null or not in javascript?It is right way?
if ('<%=Session["Time"] == null%>')
{
alert('null session');
}
Here's a solution that will test every 500 milliseconds if the user session has expired.
function CheckSession() {
var session = '<%=Session["username"] != null%>';
if (session == false) {
alert("Your Session has expired");
window.location = "login.aspx";
}
}
setInterval(CheckSession(),500);
You must try the following code:
var IsNull= '#Session["CallType"]'!= null;
Output:
IsNull value will be true or false.
I have used this code to check the Session.
public string TransactionID
{
get {
var data = ((Hashtable)(Session["SessionData"]));
if (data != null)
return data["TransactionID "];
else
return null;
}
}
if ('<%=Session["Time"] == null%>') will either evaluate to if ('True') or if ('False') which is equal to true in javascript: You may try as below:
if (<%=((Session["Time"] == null) ? 1 : 0))%>)
{
alert('null session');
}
Something like this should do it:
var isNullSession = <%=(Session["time"]==null).ToString().ToLower()%>;
if (isNullSession) {
alert('A null session variable I be');
}
This wouldn't mean the "Session" was null, just the session variable "time".
Steve
Here is the procedure to check whether session is null or not in Javascript. we have to do something like this.
var sessionValue = '<%=Session["Time"] != null%>';
So here is the point, if Session["Time"] is not null then it will return
'True' which is a string in this case. so then we can do our further
processing in this manner.
if (sessionValue == 'True')
{
alert('session is not null');
}
else
{
alert('session is null');
}

Can't seem to catch javascript being null

I have the following code, and i'm trying to protect when an error happens / no data is returned. As it stands, I get the error:
[Info] undefined (ionic.bundle.js, line 19387)
[Error] Error: undefined is not an object (evaluating 'data.data.length')
The code is like the following, understandably it's falling over because data.data.length is null / undefined (no data returned on purpose to test)
SaveSubmitService.saveLocal('GetData', 'NearByHubs4S', $scope.options, false).then(function (data) {
$scope.return = data;
if (typeof data != "undefined") {
if (data.data.length > 0) {
$scope.bars = $scope.return.data;
$scope.noNot = false;
} else {
$scope.noNot = true;
}
} else {
$cordovaDialogs.alert('Could not retrieve data. Are you sure you\'re online?', 'No Response', 'Ok');
}
$scope.$broadcast('scroll.refreshComplete');
});
Oddly, it's going through the typeof as true and falling over at data.data.length being null.
I've tried doing if data == null, data == undefined, data == "undefined", data.data.length == undefined etc.
Basically i'm trying to error if the length is null!
The problem is not that data.data.length is null. The problem is that you want to use length property on undefined. That means that data is defined and non-null, but it does not have a data property, so data.data is undefined.
Instead of:
if (typeof data != "undefined") {
if (data.data.length > 0) {
Do:
if (data && data.data) {
if (data.data.length > 0) {
It looks to me like your guard isn't sufficient, you need to check data and also data.data:
$scope.return = data;
if (data && data.data) {
if (data.data.length > 0) {
$scope.bars = $scope.return.data;
$scope.noNot = false;
} else {
$scope.noNot = true;
}
} else {
$cordovaDialogs.alert('Could not retrieve data. Are you sure you\'re online?', 'No Response', 'Ok');
}
The guard data && data.data will only be true if data is truthy (any value other than null, undefined, 0, "", NaN, or false, which are all falsy) and also if data.data is truthy. It won't try to check data.data if data is falsy.
You seem to be only checking the data variable, and not its data member.
Try using an if statement such as if (data && data.data & data.data.length > 0) to check for null data.
Try this
if (data && data.data) {
if (data.data.length > 0) {
...
} else {
....
}
}

Some problem with jquery form

This my code:
$(document).ready(function() {
$('.submit').click(function() {
var answer_text = $("#answer_text").val();
if (answer_text === '' || undefined === $("input[name='answer[scale]']:checked").val()) {
alert('error!!');
return false;
}
else {
alert('yeah! cool baby!');
}
}
});
Problem: jQuery doesn't see the ||. I don't know what to do. I tried to do something like:
if
else if
else
or
if
else
if
else
Don't know what to do. please help me, maybe some error and mistakes with OR operator? or what?
To know if no checkbox was checked just use the length, no need to mess with the value:
if (answer_text === '' || $("input[name='answer[scale]']:checked").length === 0) {
//answer text is empty and no answer scale checkbox was checked
}
i guess what you wanted to do is
if (answer_text === '' || $("input[name='answer[scale]']:checked").val()==="undefined"){
you have got the operands on the wrong side of the operator
Try to wrap it in proper braces and check.
if ((answer_text === '') || (undefined === $("input[name='answer[scale]']:checked").val()))
$(document).ready(function() {
$('.submit').click(function() {
var answer_text = $("#answer_text").val();
if (answer_text == ''){
if ( undefined === $("input[name='answer[scale]']:checked").val()){
alert('error!!');
return false;
}
else{
alert('yeah! cool baby!');
}
}
else{
alert('yeah! cool baby!');
}
}
}
This is not the fastest way but it will do it...

check that a collection is not empty

How do you check that data.results in the following is not empty before trying to perform actions on it?
$.getJSON(myurl, function(data) {
// if data.results is not empty
// {
// console.log(data.results.size);
// }
});
if (data != null && data.results != null && data.results.length > 0) {
// the array is not empty
}
I usually use something like this:
if (data != null && data.results != null && data.results.size != 0) ...
how about doing this one?
if(data!=undefined){
// do logic here
}

Categories

Resources