Bạn có thể sử dụng các tập tin cookie , window.name
, gửi dữ liệu theo url dưới dạng chuỗi truy vấn hoặc thông qua web storage.
Trong exaple này tôi sẽ lưu dữ liệu từ một trang và đọc nó từ một trang khác bằng cách sử dụng localStorage-(specs), và các phương pháp sau:
đăng nhập.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
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;
}
Đi qua các đối tượng từ trang này sang trang khác bằng cách url như chuỗi truy vấn(tìm kiếm)
login.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
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;
}
Xem một câu trả lời dài ở đây: https://stackoverflow.com/a/30070207/2247494
Khám phá [this] (http://stackoverflow.com/q/11581543/778118). Ngoài ra, có rất nhiều thông tin có sẵn về chủ đề này. Bạn sẽ nhận được google của bạn trên. – jahroy
"_Questions phải chứng minh sự hiểu biết tối thiểu về vấn đề đang được giải quyết. Hãy cho chúng tôi biết bạn đã cố gắng làm gì, tại sao nó không hoạt động và cách hoạt động._" Bỏ phiếu để đóng. – jahroy
Được gắn cờ trùng lặp: * – Jack