使用Localstorage存储和检索从一个html页面到另一个html页面的javascript变量(DOM) [英] Using Localstorage to store and retrieve javascript variables from one html page to another (DOM)

查看:132
本文介绍了使用Localstorage存储和检索从一个html页面到另一个html页面的javascript变量(DOM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html页面 1.html 我希望获得一些文本内容并将其存储到 js.js 使用jquery来获取id的文本。

I have one html page 1.html and i want to get some text content and store it to a js.js file using jquery to get the text by id.

此代码仅适用于 1.html 页面,我要复制的文字,但 2.html 文件中

This code only works in 1.html page, where the text I want to copy from is but not in 2.html file.

这是我的代码。请注意,如果我将文本存储在localstorage setter第二个参数中,它就可以工作。

Here is my code. Note that it works if I store text inside localstorage setter second parameter.

 $( 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

Here is the complete demo I am working on Github:

推荐答案

你需要使用 localStorage cookies

关于第一页,使用以下代码:

On the first page, use the following code:

localStorage.setItem("variableName", "variableContent")

为域设置 variableName 的localStorage变量,包含内容 variableContent 您可以将这些名称和值更改为您想要的任何内容,它们仅用作示例

That sets a localStorage variable of variableName for the domain, with the content variableContent. You can change these names and values to whatever you want, they are just used as an example

在第二页上,使用值获取值以下代码:

On the second page, get the value using the following code:

localStorage.getItem("variableName")

这将返回存储在 variableName 中的值,即 variableContent

That will return the value stored in variableName, which is variableContent.


  • 金额限制为5MB您可以存储在localStorage中的数据。

  • 您可以使用 localStorage.removeItem(variableName)删除项目。

  • 根据您所处的位置,您可能需要查看 cookie策略(这会影响在计算机上存储数据的所有形式的网站,而不仅仅是cookie)。 这很重要,因为否则使用任何这些解决方案可能都是非法的。

  • 如果您只想存储数据,直到用户关闭浏览器,您就可以使用sessionStorage(只需在代码的所有实例中将 localStorage 更改为 sessionStorage

  • 如果您希望能够在服务器上使用变量值,请使用cookie - 请查看 MDN上的cookie

  • 有关localStorage的更多信息,请查看关于MDN的这篇文章,或 W3Schools上的这个

  • There is a 5MB limit on the amount of data you can store in localStorage.
  • You can remove items using localStorage.removeItem("variableName").
  • Depending on where you are, you may need to take a look at the cookie policy (this effects all forms of websites storing data on a computer, not just cookies). This is important, as otherwise using any of these solutions may be illegal.
  • If you only want to store data until the user closes their browser, you can use sessionStorage instead (just change localStorage to sessionStorage in all instances of your code)
  • If you want to be able to use the variable value on the server as well, use cookies instead - check out cookies on MDN
  • For more information on localStorage, check out this article on MDN, or this one on W3Schools

这篇关于使用Localstorage存储和检索从一个html页面到另一个html页面的javascript变量(DOM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆