![](/img/trans.png)
[英]Pass javascript variables from a html page to another using local storage
[英]Using Localstorage to store and retrieve javascript variables from one html page to another (DOM)
我有一個html頁面1.html
,我想得到一些文本內容,並使用jquery將其存儲到js.js
文件,以獲取id的文本。
此代碼僅適用於1.html
頁面, 其中我要復制的文本 不在 2.html
文件中。
這是我的代碼。 請注意,如果我將文本存儲在localstorage setter第二個參數中,它就可以工作。
$( document ).ready(function() {
var c1Title= $('#r1c1Title').text();
//changing c1Title to any String content like "test" will work
localStorage.setItem("t1",c1Title);
var result = localStorage.getItem("t1");
$("#title1").html(result);
alert(result);
});
這是我在Github上工作的完整演示:
您需要使用localStorage或cookie 。
在第一頁上,使用以下代碼:
localStorage.setItem("variableName", "variableContent")
這集的localStorage的變量variableName
域,與內容variableContent
。 您可以將這些名稱和值更改為您想要的任何名稱和值,它們僅用作示例
在第二頁上,使用以下代碼獲取值:
localStorage.getItem("variableName")
這將返回存儲在variableName
的值,即variableContent
。
localStorage.removeItem("variableName")
刪除項目。 localStorage
更改為sessionStorage
) 請嘗試使用此代碼。 最好使用本地存儲 。
在這里,您需要確保在父html頁面或父js文件中設置此本地存儲值。
創建本地存儲
localStorage.setItem(“{itemlable}”,{itemvalue});
localStorage.setItem("variable1", $('#r1c1Title').text());
localStorage.setItem("variable2", $('#r1c2Title').text());
獲取本地存儲價值
localStorage.getItem( “{itemlable}”)
alert(localStorage.getItem("variable1") + ' Second variable ::: '+ localStorage.getItem("variable2"));
有關更多信息,請訪問此鏈接https://www.w3schools.com/html/html5_webstorage.asp
如果您想存儲在div中,請遵循此代碼。
Html代碼
<div class="div_data"></div>
Js代碼:
$(document).ready(function () {
localStorage.setItem("variable1", "Value 1");
localStorage.setItem("variable2", "Value 2");
$(".div_data").html(' First variable ::: '+localStorage.getItem("variable1") + ' Second variable ::: '+ localStorage.getItem("variable2"));
});
希望這可以幫助。
您可以使用上述注釋中提到的本地存儲。 請在下面找到如何用javascript編寫。
本地存儲優點和缺點
優點:
缺點:
它適用於同源策略。 因此,存儲的數據只能在同一個來源上使用。 //將值存儲在本地存儲localStorage.setItem(“c1Title”,$('#r1c1Title')。text());
// Retrieve value in local storage
localStorage.getItem("c1Title");
你的html div
<div id="output"></div>
添加Javascript代碼
$('#output').html(localStorage.getItem("c1Title"));
如果它不起作用,請告訴我
創建一個common.js文件並修改它並保存。
Session = (function () {
var instance;
function init() {
var sessionIdKey = "UserLoggedIn";
return {
// Public methods and variables.
set: function (sessionData) {
window.localStorage.setItem(sessionIdKey, JSON.stringify(sessionData));
return true;
},
get: function () {
var result = null;
try {
result = JSON.parse(window.localStorage.getItem(sessionIdKey));
} catch (e) { }
return result;
}
};
};
return {
getInstance: function () {
if (!instance) {
instance = init();
}
return instance;
}
};
}());
function isSessionActive() {
var session = Session.getInstance().get();
if (session != null) {
return true;
}
else {
return false;
}
}
function clearSession() {
window.localStorage.clear();
window.localStorage.removeItem("CampolUserLoggedIn");
window.location.assign("/Account/Login.aspx");
}
像這樣插入。
$(function () {
if (!isSessionActive()) {
var obj = {};
obj.ID = 1;
obj.Name = "Neeraj Pathak";
obj.OrganizationID = 1;
obj.Email = "npathak56@gmail.com";
Session.getInstance().set(obj);
}
///clearSession();
});
得到這樣的方式
LoggedinUserDetails = Session.getInstance().get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.