Wilson
Wilson

Reputation: 8768

How to execute javascript in different file?

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

Answers (4)

Scorpion-Prince
Scorpion-Prince

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

Marc
Marc

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

joshuahealy
joshuahealy

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

Dr.Kameleon
Dr.Kameleon

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

Related Questions