Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中将变量从一个文件发送到另一个文件?_Javascript_Jquery_Html - Fatal编程技术网

如何在Javascript中将变量从一个文件发送到另一个文件?

如何在Javascript中将变量从一个文件发送到另一个文件?,javascript,jquery,html,Javascript,Jquery,Html,我想将用户名和密码从页面login.html发送到index.html。我怎样才能尽可能容易地做到这一点?如何对字符串进行编码,使其为URL编码和UTF-8 干杯!对你的肚子很好吃 这很有趣,但这里有一个真正的答案。您可能希望将信息存储在cookie中。这是一种将信息从web应用程序的一部分传递到另一部分的好方法。这是一种常见的技术,因此所有平台都能很好地支持它 记住cookies是公开的,所以不要放任何可能被黑客攻击的信息。您应该对cookie中的值进行哈希和/或加密。您还可以生成随机信息—

我想将用户名和密码从页面
login.html
发送到
index.html
。我怎样才能尽可能容易地做到这一点?如何对字符串进行编码,使其为URL编码和UTF-8


干杯!对你的肚子很好吃


这很有趣,但这里有一个真正的答案。您可能希望将信息存储在cookie中。这是一种将信息从web应用程序的一部分传递到另一部分的好方法。这是一种常见的技术,因此所有平台都能很好地支持它


记住cookies是公开的,所以不要放任何可能被黑客攻击的信息。您应该对cookie中的值进行哈希和/或加密。您还可以生成随机信息——这是最好的。例如,当用户登录时,生成一个随机数并将该数字和用户ID存储在表中,然后将该随机数作为cookie传递。通过这种方式,只有您的DB拥有信息,您无法破解cookie(例如,通过添加cookie)。

您可以使用
encodeURI('some text to encode')
到问题的第二部分

您可以使用jquery cookie插件:

用法:

<script src="/path/to/jquery.cookie.js"></script>
创建过期cookie,7天后:

$.cookie('cookieName', 'myValue', { expires: 7 });
阅读cookie:

$.cookie('cookieName'); // => "myValue"
删除cookie:

$.removeCookie('cookieName');

您可以使用cookies
window.name
,通过url作为querystring发送数据,或通过

在本例中,我将使用-()和以下方法从一个页面保存数据,并从另一个页面读取数据:

login.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);
}
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;
}
function saveData(user, pass) {
   var account = {
     User: user,
     Pass: pass
   };
   account = JSON.stringify(account);
   account = btoa(account);
   location.assign("index.html?a=" + account);
}
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;
}
index.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);
}
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;
}
function saveData(user, pass) {
   var account = {
     User: user,
     Pass: pass
   };
   account = JSON.stringify(account);
   account = btoa(account);
   location.assign("index.html?a=" + account);
}
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;
}

通过url将对象从一个页面传递到另一个页面,作为querystring(搜索)

login.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);
}
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;
}
function saveData(user, pass) {
   var account = {
     User: user,
     Pass: pass
   };
   account = JSON.stringify(account);
   account = btoa(account);
   location.assign("index.html?a=" + account);
}
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;
}
index.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);
}
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;
}
function saveData(user, pass) {
   var account = {
     User: user,
     Pass: pass
   };
   account = JSON.stringify(account);
   account = btoa(account);
   location.assign("index.html?a=" + account);
}
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;
}


请参阅此处的扩展答案:

签出。此外,关于这个主题有很多信息。你应该打开谷歌。“问题必须表明你对正在解决的问题有一个最低限度的理解。告诉我们你试过做什么,为什么它不起作用,以及它应该如何起作用。”投票结束。为了跟进@jahroy,@jahroy,你们为什么标记为重复?在给定的URL中没有公认的正确答案。您可以在此处找到完整答案:显然是基于技术优势的最佳答案;-)(我不是落选者)@jahroy我已经研究饼干多年了@霍根和它显示!Jack-Cookie不是为了安全,所以要小心存储在其中的内容(即用户名和密码)@nick-我总是对巧克力片进行哈希和加密。@nick:除非使用HTTPS,否则没有其他东西是安全的。只有Cookie通过网络发送的频率比表单中的POST参数更高,因此更容易嗅探。我使用了那个插件,它很好!但您不需要cookies,只需使用[]保存本地数据。这<代码>var帐户=localStorage.getItem(“U帐户”);如果(!账户)返回错误…要求在检查localStorage是否存在之前加载任何数据,而这<代码>如果(localStorage中的帐户){//do something…只需检查您的数据密钥是否存在于localStorage中。它为您带来的处理时间可以忽略不计,但它为您节省了返回语句和(imho)看起来更干净。谢谢你的建议@MistyDawn,检查存储中的属性确实比获取与密钥关联的数据要快。