简体   繁体   English

使用javascript在多个页面中获得相同的值

[英]Get the same value in multiple pages using javascript

I have multiple pages, 我有多页,

login.html
 -a.html
 -b.html
 .
 .
 -z.html

when user login,I want all this page can get the user information,like user_id and user_name. 当用户登录时,我希望所有页面都能获取用户信息,例如user_id和user_name。 but without session,how can i do this only in javascript? 但没有会话,我该如何仅使用javascript?

There are many ways to solve your problem: 有很多方法可以解决您的问题:

Browser's cookies 浏览器的cookie

You can use the browser cookie to get data of a specific domain, like you can see on this W3 school page . 您可以使用浏览器cookie获取特定域的数据,就像在W3学校页面上看到的那样。 One attempt is that you are able to read cookies using your server side 一种尝试是您能够使用服务器端读取Cookie

To set a cookie's browser you can do something like this: 要设置Cookie的浏览器,您可以执行以下操作:

var user_name = "John Doe"
var user_id = 1235456
document.cookie = "user_name=" + user_name;
document.cookie = "user_id=" + user_id;

To get the data you can use this function, created on kirlich's answer : 要获取数据,您可以使用在kirlich的答案上创建的此函数:

function getCookie(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
}

getCookie("user_name"); // => John Doe

Local Storage 本地存储

Local storage has a difference, its can only be read by the client side. 本地存储有所不同,它只能由客户端读取。 Turns your data safe against Cross-site request forgery : 使您的数据免受跨站伪造的威胁

localStorage.setItem("user_name", "John Doe");
localStorage.getItem("user_name"); // => "John Doe"

Session Storage 会话存储

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. sessionStorage对象与localStorage对象相同,只不过它仅存储一个会话的数据。 The data is deleted when the user closes the specific browser tab. 当用户关闭特定的浏览器选项卡时,数据将被删除。

sessionStorage.username = "John Doe";
console.log(sessionStorage.username); // => "John Doe"

Use localStorage 使用localStorage

var userId = document.getElementById('userId').value;
localStorage.setItem('User Id', userId);

Then on each html page get the User Id with: 然后在每个html页面上获取具有以下内容的用户ID:

var someVariable = localStorage.getItem('User Id');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用相同的JavaScript / CSS脚本的多个ASPX页面 - Multiple ASPX Pages using the same JavaScript / CSS Scripts 如何使用JavaScript路由为多个页面使用同一页面特定代码? - How to the same page specific code for multiple pages using javascript routes? 如何在各个页面中具有相同 ID 的多个页面上使用 JavaScript 隐藏/显示链接(元素)? - How to hide/show links (elements) using JavaScript on multiple pages having same id in respective pages? 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 在多个页面中具有相同功能名称的Javascript回调 - Javascript CallBack with same function name in multiple pages 在多个浏览器实例中打开的同一网站的页面应使用 javascript 引用同一变量 - Pages of same website opened in multiple instance of browser should refer to same variable using javascript 如何在Javascript中的多个浏览器中获得相同的值? - How to get a value to be the same across multiple browsers in Javascript? 使用 javascript 检查和删除数组中相同值的多次出现 - Checking and deleting multiple occurrences of the same value in an array using javascript 获取无线电值以使用Java脚本继承页面 - Get Radio Value to Carry over pages with Javascript ASP.NET hiddenField使用ClientID获得价值,就像使用javascript使用母版页一样 - ASP.NET hiddenField get value using ClientID as using master pages using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM