Way of Using JavaScript

Advertisements

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 Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements