简体   繁体   English

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

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

I have a PhoneGap application which displays an RSS feed. 我有一个显示RSS提要的PhoneGap应用程序。 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. 但是,在首次安装该应用程序时,它应显示常规的RSS源。 For some reason the RSS feed comes up blank until a category is selected. 由于某种原因,RSS feed变成空白,直到选择了类别。 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
var URL = localStorage.getItem('url') || 'http://www.RssUrlHere.com';

|| is OR operator. 是OR运算符。 It starts evaluating from left, if left most one is falsy value the result will be the one in right side. 它从左开始评估,如果最左边的是falsy值,结果将是右边的。

also when working with undefined, the format is different 在使用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);

try using 尝试使用

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM