jaffar
jaffar

Reputation: 671

How can I use a variable of one <script> in another <script> in javascript?

How can I use a variable of one in another in javascript?

Upvotes: 14

Views: 19280

Answers (4)

Majid
Majid

Reputation: 14243

When you declared a variable inside a <script> , you can access it anywhere simply by using this:

var myValue= window.yourProperty;

Upvotes: 0

Goodness Ngoveni
Goodness Ngoveni

Reputation: 61

If the two script tags are on the same page, any variable declared outside of the scope of a function is global to the page. However, it does matter which script is loaded first for some applications.

Upvotes: 6

seth
seth

Reputation: 37267

Make it global;

script1.js

 var foobar = "O HAI";

script2.js

  alert(foobar);

Upvotes: 10

slolife
slolife

Reputation: 19870

If the two script tags are on the same page, any variable declared outside of the scope of a function is global to the page.

Upvotes: 17

Related Questions