Way of Using JavaScript
Advertisements
Way of Using JavaScript
There are three places to put the JavaScript code.
- Between the <body> </body> tag of html (Inline JavaScript)
- Between the <head> </head> tag of html (Internal JavaScript)
- In .js file (External JavaScript)
Inline JavaScript
When java script was written within the html element using attributes related to events of the element then it is called as inline java script.
Example of Inline JavaScript
Example How to use JavaScript
<html> <form> <input type="button" value="Click" onclick="alert('Button Clicked')"/> </form> </html>
Result
Internal JavaScript
When java script was written within the
section using element then it is called as internal java script.Example of Internal JavaScript
Example
<html> <head> <script> function msg() { alert("Welcome in JavaScript"); } </script> </head> <form> <input type="button" value="Click" onclick="msg()"/> </form> </html>
Result
External JavaScript
Writing java script in a separate file with extension .js is called as external java script. For adding the reference of an external java script file to your html page, use tag with src attribute as follows
Example
<script type="text/javascript" src="filename.js"/>
Create a file with name functions.js and write the following java script functions in it.
message.js
Example
function msg() { alert("Welcome in JavaScript"); }
Create a html page and use the file functions.js as follows
index.html
Example
<html> <head> <script type="text/javascript" src="message.js"></script> </head> <body> <form> <input type="button" value="click" onclick="msg()"/> </form> </body> </html>
Result
Google Advertisment