首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果本地存储为空或为空,如何显示内容

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

Stack Overflow用户
提问于 2014-02-20 20:34:49
回答 2查看 132关注 0票数 0

我有一个PhoneGap应用程序,它显示一个RSS提要。我使用本地存储为用户保存一个最喜欢的类别,并在应用程序加载时从本地存储中调用它。然而,当应用程序第一次安装时,它应该显示一般的RSS提要。由于某些原因,RSS提要显示为空白,直到选择了一个类别。然后,当重新加载应用程序时,它可以正常工作。有人能帮忙吗?谢谢!

这里是我必须检查当地仓库的东西:

代码语言:javascript
运行
复制
//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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-20 20:39:10

代码语言:javascript
运行
复制
var URL = localStorage.getItem('url') || 'http://www.RssUrlHere.com';

||是OR算子。它从左开始计算,如果最左边的值是falsy值,则结果将是右侧的结果。

同样,当使用未定义的时,格式也不同。

代码语言:javascript
运行
复制
if(typeof x === "undefined"){
}

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2014-02-20 20:38:09

试着使用

代码语言:javascript
运行
复制
if (!typeof rssURL == "undefined" || rssURL !== "null") { URL = rssURL }

而不是

代码语言:javascript
运行
复制
if (rssURL !== "undefined" || rssURL !== "null") { URL = rssURL }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21918942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档