0

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?

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I put them in the head, and do I have to specify something like global? Or "var x,y" is fine?
vars in the global scope (outside any functions) are global.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.