可以在大多數現代瀏覽器中看到http://www.w3schools.com/html/html5_webstorage.asp
,如果你打算支持舊的瀏覽器,你可以設置一個cookie,然後當讀它使用localStorage的文件重新加載。 http://www.w3schools.com/js/js_cookies.asp
編輯:試試這個代碼
//function to check support for localStorage
function checkLocalStorageSupport() {
try {
localStorage.setItem("x", "x");
localStorage.removeItem("x");
return true;
} catch (e) {
return false;
}
}
//function to read value of cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
if (checkLocalStorageSupport()) {
if (localStorage.getItem('a') != null)
if (localStorage.getItem('a') == "true") //check that the value of localStorage is not null
document.write("foo"); //do operation if localStorage value is true
else
document.write("bar");
else
localStorage.setItem('a', 'false');
}
else {
if (getCookie('a') != null) //check that the value of cookie is not null
if (getCookie('a') == "true")
document.write("foo"); //do operation if cookie value is true
else
document.write("bar");
else
document.cookie = "a=false; expires=Thu, 31 Dec 2015 12:00:00 UTC"; //if no cookie exists set a cookie
}
無需修改源代碼?沒有。通過修改腳本,使其從'localStorage','sessionStorage',cookies,AJAX讀取狀態值,或者使用由服務器端語言提供的值?是。 – zzzzBov 2015-04-02 13:40:14