[英]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: 如果您希望这些对于每个客户都是唯一的,则可以:
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. sessionStorage
和localStorage
对象将允许您在.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.