I have the following two javascript functions:
1
showCountry()
2
showUser()
I would like to put them in external ".js" files
1
<a href="javascript:showCountry('countryCode')">countryCode</a>
2
<form> <select name="users" onChange="showUser(this.value)"> <option value="1">Tom</option> <option value="2">Bob</option> <option value="3">Joe</option> </select> </form>
What is the correct syntax to call these functions?
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.
The use of external JavaScript is more practical when the same code is to be used in many different web pages. Using an external script is easy , just put the name of the script file(our . js file) in the src (source) attribute of <script> tag.
To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.
Code like this
<html> <head> <script type="text/javascript" src="path/to/script.js"></script> <!--other script and also external css included over here--> </head> <body> <form> <select name="users" onChange="showUser(this.value)"> <option value="1">Tom</option> <option value="2">Bob</option> <option value="3">Joe</option> </select> </form> </body> </html>
I hope it will help you.... thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With