Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send variables from one file to another in Javascript? [duplicate]

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?

Cheers

like image 855
Jack Avatar asked Jun 25 '13 23:06

Jack


People also ask

How do I pass a variable from one JavaScript file to another JS file?

To import a variable from another file in JavaScript: Export the variable from file A , e.g. export const str = 'Hello world' . Import the variable in file B as import { str } from './another-file. js' .

How do I pass a value from one JavaScript function to another?

When there are multiple functions (which is most of the time), there needs to be a way to pass data between the functions. This is done by passing values in parenthesis: myFunction(myData). Even when there is no data to be passed, we still have to declare and execute functions by using parenthesis: myFunction().

Can variables be reassigned in JavaScript?

In JavaScript, we can simply reassign the new value to the variable. When we change the value of a variable, we do not use var again. We simply use the = to give the variable a new value.


2 Answers

You can use cookies, window.name, send data by url as querystring, or through web storage.

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:

  • JSON.stringify()
  • JSON.parse()
  • WindowBase64.btoa()
  • WindowBase64.atob()

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);
}

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;
}

Passing the object from one page to another by url as querystring (search)

  • Location

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;
}

See an extended answer here: https://stackoverflow.com/a/30070207/2247494

like image 52
jherax Avatar answered Sep 30 '22 01:09

jherax


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.

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.)

like image 26
Hogan Avatar answered Sep 30 '22 02:09

Hogan