[英]Getting the value of a variable from localStorage from a different javascript file
在javascript中使用localstorage時遇到問題。 我有兩個javascript文件和一個多頁html文件。 我將變量的值設置為存儲在home.js類的本地存儲中,然后我想在editCase.js類中獲取相同變量的值。 這可能嗎? 這里的目標是在html文件中使用變量的幾個位置。
home.js:
var _NSCaseId;
var nsId;
$("#listOfCases li").click(function(){
nsId = $(this).attr('id');
LocalStorage.save(_NSCaseId, nsId);
alert(nsId);
});
如您所見,該值是listview項的id。 此工作正常,警報每次都顯示正確的值。
然后我想在editCase.js文件中使用相同的變量:
var id = LocalStorage.get(_NSCaseId);
alert(id);
但我在logcat中收到錯誤,說在editCase.js中未定義_NSCaseId
有什么建議?
這應該根本不起作用。 它的localStorage
(JS非常區分大小寫),為了保存項目,你使用setItem
,將你的語法更改為:
//You would not define var _NSCaseId and save the var, you save a string key
localStorage.setItem('_NSCaseId', nsId); //set
得到
localStorage.getItem('_NSCaseId');
請參閱DOM存儲: https : //developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.