Way of Using JavaScript


Prev Tutorial Next Tutorial

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


Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements