i3wangyi
i3wangyi

Reputation: 2419

How to access variable in other javascript?

Suppose Now I've a HTML file with

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

Both JS contain sets of functions, how can I access second.js from first.js?

Upvotes: 0

Views: 137

Answers (2)

Cheong BC
Cheong BC

Reputation: 316

May be you can act like this. Make it in Order.

<script type="text/javascript" src="js/first.js"></script>
<script type="text/javascript" src="js/second.js"></script>
<script>
    //call the function at first.js which will access second.js
    exampleFirstJSFunc();
</script>

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

<script> elements are executed in order. You can't access things defined in second.js while first.js is being first executed, but if you define a function in first.js and set it up so that it's called after second.js has been loaded it will see anything defined in second.js. A DOM ready or a "load" event might be a start.

Upvotes: 1

Related Questions