1

I have a function in my separate js file called hello().

On my page I have this js:

<script type='text/javascript'>//do hello fnc</script>

How can I get the on page script to call the function in my main js file?

beans
  • 1,765
  • 5
  • 25
  • 31

3 Answers3

3

Call it in the src attribute of another script tag, before referring to any of its functions in this script.

<script type='text/javascript' src='main.js'></script>
<script type='text/javascript'>
  hello();
  // Anything defined in previously included js files like main.js
  // is now available, as well as anything defined on this page.
</script>
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

Load the external file first and call the function:

<script src=external.js></script>
<script>
  hello();
</script>
Noah Freitas
  • 17,240
  • 10
  • 50
  • 67
0

You just need to include the external js file:

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

And then wherever in you code you want to call the funciton:

<script type="text/javascript">hello();</script>
Nealbo
  • 529
  • 4
  • 20