4

I have multiple javascript functions in my html page. One of them, by far the biggest, is executed on a button click. If I moved that function into a separate .js file, what would be the syntax to have the onclick run that file?

1
  • Just make sure the script file is loaded before you try to use the functions defined within it. With jquery, for example, you'd wrap your calling code within a $(function () { /* here */ });. Commented Apr 11, 2012 at 1:22

4 Answers 4

7

If the name of the function is still the same, then you don't have to do anything except include the script in your html somewhere before any code uses it, like so:

<script src="/yourscript.js" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

Comments

4

Your javascript functions can be in any number of files. You just need to add the reference to the .js file like this inside the tag:

<script type="text/javascript" src="path/to/scripts/filename.js"></script>

Comments

0

You don't have to change anything.

Just include your javascript file in your HEAD section, and the rest may remain the same. :-)

Comments

0

Your onclick doesn't run the file. It runs the function contained within that file. You wouldn't need to change the onclick syntax to make that happen.

1 Comment

Would be nice if the down-voter would say what's wrong with my answer.

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.