Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use external ".js" files

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?

like image 306
blsn Avatar asked Jul 16 '12 04:07

blsn


People also ask

How do I use an external JavaScript file?

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.

How do I run a .JS file?

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.

Why use external JS file?

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.

How do I run a .JS file in my browser?

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.


1 Answers

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

like image 164
Jalpesh Patel Avatar answered Sep 23 '22 08:09

Jalpesh Patel