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,755
  • 5
  • 24
  • 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
  • 260,803
  • 45
  • 432
  • 377
0

Load the external file first and call the function:

<script src=external.js></script>
<script>
  hello();
</script>
Noah Freitas
  • 16,872
  • 10
  • 49
  • 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
  • 499
  • 4
  • 20