![](/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.