2014-06-05 45 views
1

嗨,我得到了一個問題,我想從用戶本地存儲的值加載到一個函數,並在該功能檢查是否提供本地存儲如果它的可變負載本地用戶存儲,如果沒有加載一個頁面,這是我迄今爲止編寫的第一部分的腳本。從用戶本地存儲插入值到加載函數

 function LoadSample(num){ 
//Here is the Problem i need sone help please ;) 
     $("#code"+num).load('sites/demo'+num+'.html'); if(localStorage.userEdits!=null) 
     document.getElementById("code1").html = localStorage.userEdits; 
     $("#livebtn"+num).css("display", "none"); 
     $("#codebtn"+num).css("display", "block"); 
     $("#savebtn"+num).css("display", "none"); 
     $("#update").css("display", "none"); 


} 

function LoadCode(num){ 
    $.get('sites/demo'+num+'.html', function(data) { 
     $("#code"+num).text(""+data+""); 
     if(localStorage.userEdits!=null) 
     document.getElementById("code1").innerHTML = localStorage.userEdits; 

    }); 
    $("#codebtn"+num).css("display", "none"); 
    $("#livebtn"+num).css("display", "block"); 
    $("#savebtn"+num).css("display", "none"); 
    $("#update").css("display", "none"); 
} 



</script> 
<script> 
function saveEdits() { 

//get the editable element 
var editElem = document.getElementById("edit"); 

//get the edited element content 
var userVersion = editElem.innerHTML; 

//save the content to local storage 
localStorage.userEdits = userVersion; 

//write a confirmation to the user 
document.getElementById("update").innerHTML="Änderungen gespeichert!"; 

} 

function checkEdits() { 

//find out if the user has previously saved edits 
if(localStorage.userEdits!=null) 
document.getElementById("edit").innerHTML = localStorage.userEdits; 
} 

</script> 

回答

0

檢測,如果本地存儲可用(從最新Modernizr test):

... 
var mod = 'modernizr'; 
try { 
    localStorage.setItem(mod, mod); 
    localStorage.removeItem(mod); 
    return true; 
} catch(e) { 
    return false; 
} 
... 
+0

不尼布想如果有好吧,如果要檢查它不是也有沒有更簡單的方法? – Phil

+0

即時通訊不是很熟悉jquery請讓它更容易解釋更多;) – Phil

+0

我不想冒犯,但它有點不清楚你問的東西。你想「......檢查是否提供本地存儲,如果它的可變性」,現在你「不要neeb想檢查它,如果它有一切權利」。 –