我想将用户名和密码从页面login.html发送到index.html. 我怎样才能尽可能轻松地做到这一点?以及如何对我的字符串进行编码,使它们采用 URL 编码和 UTF-8?
干杯
我想将用户名和密码从页面login.html发送到index.html. 我怎样才能尽可能轻松地做到这一点?以及如何对我的字符串进行编码,使它们采用 URL 编码和 UTF-8?
干杯
您可以使用cookie、window.name、通过 url 作为查询字符串发送数据,或通过网络存储发送数据。
在本例中,我将使用localStorage - ( specs )和以下方法保存一页中的数据并从另一页读取它:
登录.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);
}
索引.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;
}
通过 url 将对象从一个页面传递到另一个页面作为查询字符串 (搜索)
登录.html
function saveData(user, pass) {
   var account = {
     User: user,
     Pass: pass
   };
   account = JSON.stringify(account);
   account = btoa(account);
   location.assign("index.html?a=" + account);
}
索引.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;
}
在此处查看扩展答案:https : //stackoverflow.com/a/30070207/2247494
饼干!好吃到你的肚子。
那很有趣,但这是一个真正的答案。您可能希望将信息存储在 cookie 中。这是将信息从 Web 应用程序的一个部分传递到另一个部分的好方法。这是一种通用技术,因此所有平台都很好地支持它。
请记住,cookies 是公开的,所以不要放置任何可能被黑客入侵的信息。您应该散列和/或加密 cookie 中的值。您还可以生成随机信息——这是最好的。例如,当用户登录时生成一个随机数并将该数字和用户 ID 存储在表中,然后将随机数作为 cookie 传递。通过这种方式,只有您的数据库拥有信息,而您无法破解 cookie(例如通过添加 cookie)。
您可以使用jquery-cookie插件:https : 
//github.com/carhartl/jquery-cookie
用法:
<script src="/path/to/jquery.cookie.js"></script>
创建会话cookie:
$.cookie('cookieName', 'myValue');
创建过期 cookie,7 天后:
$.cookie('cookieName', 'myValue', { expires: 7 });
读取饼干:
$.cookie('cookieName'); // => "myValue"
删除cookie:
$.removeCookie('cookieName');
您可以使用encodeURI('some text to encode')问题的第二部分