Bacon2305
Bacon2305

Reputation: 311

How do I display content if local Storage is null or empty

I have a PhoneGap application which displays an RSS feed. I am using local Storage to save a favorite category for the user, and call it from local storage when the application loads. However, when the application is first installed it should display the general RSS feed. For some reason the RSS feed comes up blank until a category is selected. Then when the application is reloaded, it works fine. Can someone help with this? Thanks!

Here is what I have to check the local storage:

//get local storage item   
var rssURL = localStorage.getItem('url'); 

//set the URL for the ajax call in case there is no stored item
var URL = 'http://www.RssUrlHere.com'; 

//if the localstorage is undefined or null, use the general RSS url , if not get assign stored URL Note: also tried != to see if that made a difference, it did not.
if (rssURL !== "undefined" || rssURL !== "null") { URL = rssURL }

//comparing the URL with category URLs, works fine but clunky!
                if(URL == 'http://www.RssUrlHere.comA') { $('#header-title').html("Bioengineering"); $('#event-title').html("Bioengineering");}
                else if (URL == 'http://www.RssUrlHere.comB') { $('#header-title').html("Communications"); $('#event-title').html("Communications");}
                else if (URL == 'http://www.RssUrlHere.comC') { $('#header-title').html("Electrical/Power"); $('#event-title').html("Electrical/Power");}
                else if (URL == 'http://www.RssUrlHere.comD') { $('#header-title').html("Electronics Design"); $('#event-title').html("Electronics Design");}
                else if (URL == 'http://www.RssUrlHere.comE') { $('#header-title').html("Nanoengineering"); $('#event-title').html("Nanoengineering");}
                else if (URL == 'http://www.RssUrlHere.comF') { $('#header-title').html("Optics/Display"); $('#event-title').html("Optics/Display");}
                else if (URL == 'http://www.RssUrlHere.comG') { $('#header-title').html("Semiconductors"); $('#event-title').html("Semiconductors");}
                else if (URL == 'http://www.RssUrlHere.comH') { $('#header-title').html("Computers/Software"); $('#event-title').html("Computers/Software");}
                else if (URL == 'http://www.RssUrlHere.comI') { $('#header-title').html("Technology Management"); $('#event-title').html("Technology Management");}
                else { $('#event-title').html("All Events"); $('#header-title').html("Mobile");}

//My ajax call with for the URL     
    $.ajax({
                type: 'GET',
                url: URL,
                dataType: 'xml',
                success: function (xml) {


                         //functions here

Upvotes: 0

Views: 162

Answers (2)

sabithpocker
sabithpocker

Reputation: 15576

var URL = localStorage.getItem('url') || 'http://www.RssUrlHere.com';

|| is OR operator. It starts evaluating from left, if left most one is falsy value the result will be the one in right side.

also when working with undefined, the format is different

if(typeof x === "undefined"){
}

var title = "General";

switch(URL){
case 'http://www.RssUrlHere.comB' :title="Bioengineering"; break;
case 'http://www.RssUrlHere.comC' :title="dfsfsdf"; break;
case 'http://www.RssUrlHere.comD' :title="sdfsdf"; break;
case 'http://www.RssUrlHere.comE' :title="sdfsdf"; break;
case 'http://www.RssUrlHere.comF' :title="sdfsdfsdfsdf"; break;
case 'http://www.RssUrlHere.comG' :title=""; break;
case 'http://www.RssUrlHere.comH' :title="sdfsdf"; break;
case 'http://www.RssUrlHere.comI' :title="sdfsdfsdf"; break;
default : title = "Unknown Title";
}

$('#header-title').html(title);
$('#event-title').html(title);

Upvotes: 1

LaczkoUr
LaczkoUr

Reputation: 107

try using

if (!typeof rssURL == "undefined" || rssURL !== "null") { URL = rssURL }

instead of

if (rssURL !== "undefined" || rssURL !== "null") { URL = rssURL }

Upvotes: 0

Related Questions