How to Send Variables from One File to Another in JavaScript

How to pass variable from one Javascript to another Javascript file

see about local and global variables for more info. http://www.w3schools.com/js/js_scope.asp.

Make sure your var X is not inside a function and that your file is load in the correct order.

<script src="file1.js"><script> //declare var x=1 here
<script src="file2.js"><script> // you can access x from here.

Call variables from one javascript file to another

First, instead of

var VAR = new Object;
VAR.myvalue = "Yeah!";

Opt for

var VAR = {
myvalue: "Yeah!"
};

But so long as var.js is referenced first, before sample.js, what you have should work fine.

var.js will declare, and initialize VAR, which will be read from the script declared in sample.js

Pass Variable To Another file

The 2 files should be on the same location and php is installed in your server

A.html

...<a href="B.php?data1=1654&data2=string">Button</a>...

How to send variables from one file to another in Javascript?

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

Can I access variables from another file?

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

// first.js
var colorCodes = {
back : "#fff",
front : "#888",
side : "#369"
};

... in another file ...

// second.js
alert(colorCodes.back); // alerts `#fff`

... in your html file ...

<script type="text/javascript" src="first.js"></script> 
<script type="text/javascript" src="second.js"></script>


Related Topics



Leave a reply



Submit