如果本地存储为空或空,如何显示内容

时间:2014-02-20 20:34:49

标签: javascript html cordova local-storage

我有一个显示RSS源的PhoneGap应用程序。我使用本地存储为用户保存喜欢的类别,并在加载应用程序时从本地存储调用它。但是,首次安装应用程序时,它应显示常规RSS源。 出于某种原因,在选择类别之前,RSS源会显示为空白。然后当重新加载应用程序时,它工作正常。有人可以帮忙吗?谢谢!

以下是我必须检查本地存储的内容:

//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

2 个答案:

答案 0 :(得分:1)

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

||是OR运算符。它从左开始评估,如果剩下的最多一个是falsy值,则结果将是右侧的结果。

使用undefined时,格式也不同

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

答案 1 :(得分:0)

尝试使用

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

而不是

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