jQuery 如何在Javascript中将变量从一个文件发送到另一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17309199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send variables from one file to another in Javascript?
提问by Hyman
I want to send a username and password from page login.html
to index.html
. How can I do that as easy as possible? And how to I encode my strings so they're URL-encoded and UTF-8?
我想将用户名和密码从页面login.html
发送到index.html
. 我怎样才能尽可能容易地做到这一点?以及如何对我的字符串进行编码,使它们采用 URL 编码和 UTF-8?
Cheers
干杯
回答by jherax
You can use cookies, window.name
, send data by url as querystring, or through web storage.
您可以使用cookie、window.name
、通过 url 作为查询字符串发送数据,或通过网络存储发送数据。
In this exaple I'm going to save data from one page and read it from another page using the localStorage- (specs),and the following methods:
在本例中,我将使用localStorage- ( specs)和以下方法保存一页中的数据并从另一页读取数据:
login.html
登录.html
function saveData(user, pass) {
var account = {
User: user,
Pass: pass
};
//converts to JSON string the Object
account = JSON.stringify(account);
//creates a base-64 encoded ASCII string
account = btoa(account);
//save the encoded accout to web storage
localStorage.setItem('_account', account);
}
index.html
索引.html
function loadData() {
var account = localStorage.getItem('_account');
if (!account) return false;
localStorage.removeItem('_account');
//decodes a string data encoded using base-64
account = atob(account);
//parses to Object the JSON string
account = JSON.parse(account);
//do what you need with the Object
fillFields(account.User, account.Pass);
return true;
}
Passing the object from one page to another by url as querystring(search)
通过 url 将对象从一个页面传递到另一个页面作为查询字符串(搜索)
login.html
登录.html
function saveData(user, pass) {
var account = {
User: user,
Pass: pass
};
account = JSON.stringify(account);
account = btoa(account);
location.assign("index.html?a=" + account);
}
index.html
索引.html
function loadData() {
var account = location.search;
if (!account) return false;
account = account.substr(1);
//gets the 'a' parameter from querystring
var a = (/^a=/);
account = account.split("&").filter(function(item) {
return a.test(item);
});
if (!account.length) return false;
//gets the first element 'a' matched
account = account[0].replace("a=", "");
account = atob(account);
account = JSON.parse(account);
//do what you need with the Object
fillFields(account.User, account.Pass);
return true;
}
See an extended answer here: https://stackoverflow.com/a/30070207/2247494
在此处查看扩展答案:https: //stackoverflow.com/a/30070207/2247494
回答by Hogan
Cookies! Yummy for your tummy.
饼干!好吃到你的肚子。
That was fun but here is a real answer. You probably want to store information in a cookie. This is a good way to pass information from one part of your web application to another. It is a common technique so all platforms support it well.
那很有趣,但这是一个真正的答案。您可能希望将信息存储在 cookie 中。这是将信息从 Web 应用程序的一个部分传递到另一个部分的好方法。这是一种通用技术,因此所有平台都很好地支持它。
Remember cookies are public so don't put any information that can be hacked. You should hash and or encrypt the value in a cookie. You can also generate random information -- this is best. For example, when a user logs in generate a random number and store that number and the user ID in a table then pass the random number as the cookie. In this way only your DB has the information and you can't hack the cookie (by adding one for example.)
请记住 cookie 是公开的,因此不要放置任何可能被黑客入侵的信息。您应该散列和/或加密 cookie 中的值。您还可以生成随机信息——这是最好的。例如,当用户登录时生成一个随机数并将该数字和用户 ID 存储在一个表中,然后将随机数作为 cookie 传递。通过这种方式,只有您的数据库拥有信息,而您无法破解 cookie(例如通过添加 cookie)。
回答by Hogan
You can use the jquery-cookieplugin:
https://github.com/carhartl/jquery-cookie
您可以使用jquery-cookie插件:https:
//github.com/carhartl/jquery-cookie
Usage:
用法:
<script src="/path/to/jquery.cookie.js"></script>
Create session cookie:
创建会话cookie:
$.cookie('cookieName', 'myValue');
Create expiring cookie, 7 days from then:
创建过期 cookie,7 天后:
$.cookie('cookieName', 'myValue', { expires: 7 });
Read cookie:
读取饼干:
$.cookie('cookieName'); // => "myValue"
Delete cookie:
删除cookie:
$.removeCookie('cookieName');
回答by Jaime
You can use encodeURI('some text to encode')
to the second part of your question
您可以使用encodeURI('some text to encode')
问题的第二部分