Reputation: 8768
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?
Upvotes: 3
Views: 19213
Reputation: 3634
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>
Upvotes: 4
Reputation: 11613
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.
Upvotes: 0
Reputation: 3569
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>
Upvotes: 6
Reputation: 22820
You don't have to change anything.
Just include your javascript file in your HEAD
section, and the rest may remain the same. :-)
Upvotes: 0