简体   繁体   English

使用javascript或jquery在不同的html文件之间共享变量

[英]share variables between different html file using javascript or jquery

What is the simplest way to define and access shared variables between .html files belonging to the same project. 在属于同一项目的.html文件之间定义和访问共享变量的最简单方法是什么。

Thank you all, 谢谢你们,

b regards 问候

If you intend for these to be unique for each client, you could: 如果您希望这些对于每个客户都是唯一的,则可以:

  1. Store cookies 储存Cookie
  2. Pass data as part of the request parameters 将数据作为请求参数的一部分传递

If you intend to store data permanently, you should use a server side language. 如果打算永久存储数据,则应使用服务器端语言。

The variables you define in any JS file are accessed in exactly the same way. 您在任何JS文件中定义的变量的访问方式都完全相同。 For example, consider the following variables.js file which you include at the top of all your pages: 例如,考虑以下包含在所有页面顶部的variables.js文件:

var variable1 = "foo";
var variable2 = "bar";

Then, in any script that comes after you can simply refer to these variables. 然后,在之后的任何脚本中,您都可以简单地引用这些变量。 Consider mypage.html: 考虑一下mypage.html:

<html>
    <head>
        <script src="variables.js" type="text/javascript"></script>
        <script>
            alert(variable1); // alerts "foo"
        </script>
    </head>
    <body>
    </body>
</html>

The sessionStorage and localStorage objects will allow you to share key/string value pairs between .html files, provided they belong to the same domain. sessionStoragelocalStorage对象将允许您在.html文件之间共享键/字符串值对,前提是它们属于同一域。 localStorage values are stored in the browser between sessions, and if the user switches browsers the values will not be carried with them; localStorage值存储在会话之间的浏览器中,如果用户切换浏览器,则这些值将不随身携带; nor can the server view these values without extra code. 服务器也无法在没有额外代码的情况下查看这些值。

http://en.wikipedia.org/wiki/Web_storage http://en.wikipedia.org/wiki/Web_storage

暂无
暂无

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

相关问题 在 html 页面之间共享变量 - Share variables between html pages 访问JAVASCRIPT中声明的变量在不同的HTML文件中 - Accesing variables declared in JAVASCRIPT in a different HTML FILE 如何使用单个Javascript文件在两个或更多HTML页面之间共享本地存储,而不会出现“空”的情况? - How to share local storage between two or more HTML pages using a single Javascript file and not getting “null”? 使用 jquery/javascript 附加带有变量的 html 代码 - Append html code with variables using jquery / javascript 在不使用HTML的情况下与其他Java脚本共享JavaScript文件资源 - Share JavaScript file resources with another Javascript without using HTML 如何在一个是模块的 Javascript 文件和另一个不是模块的 Javascript 文件之间共享函数和变量 - How to share functions and variables between one Javascript file that IS a module, and another Javascript file that is NOT a module 有没有某种方法可以在jover的hover()中的两个函数之间共享变量而不使用全局变量? - Is there some way to share variables between the two functions in hover() in jquery without using global variables? 如何在不同的javascript模块中共享变量 - how to share variables in different javascript modules 在不同的 .js 文件之间传递变量; 在同一个 html 文件中 - Pass variables between different .js files; inside the same html file 在HTML中使用JavaScript变量 - Using javascript variables in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM