2014-02-20 42 views
0

我有一個顯示RSS提要的PhoneGap應用程序。我正在使用本地存儲爲用戶保存一個最喜歡的類別,並在應用程序加載時從本地存儲中調用它。但是,首次安裝應用程序時,應該顯示一般的RSS提要。 由於某種原因,RSS Feed在選擇類別之前爲空白。然後,當應用程序重新加載時,它工作正常。有人可以幫忙嗎?謝謝!如果本地存儲爲空或空的情況下如何顯示內容

這是我要檢查本地存儲:

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

回答

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

||是OR運算符。它從左開始評估,如果剩下最多的一個是falsy值,結果將是右側的一個。

還與不確定的工作時,格式不同

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

釘了它!做得好!我不知道我可以編寫這樣的變量,所以謝謝你通知我! – Bacon2305

+0

哇!謝謝你也幫助我!看起來更好,功能更好。真的很感激它。 – Bacon2305

+0

很高興它幫助:) – sabithpocker

0

嘗試使用,而不是

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

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

沒有運氣與此有關。仍然是空白內容部分onload。但是,謝謝你的迴應! – Bacon2305

相關問題