Advertisement

Responsive Advertisement

JAVA SCRIPT

 Remove this site banner with a paid plan


JAVA SCRIPT

JavaScript is a programming language that is commonly used to add interactivity and other dynamic features to websites. It can run on the client side (in a web browser) or on the server side (using Node.js). It is often used in conjunction with HTML and CSS to create complex web pages and web applications.

EXAMPLE 1

<!DOCTYPE html>
<html>
<head>
<title>Hello World in JavaScript</title>
</head>
<body>
<script type="text/javascript"> document.write("Hello World ");
</script>
</body>
</html>



EXAMPLE 2

<!DOCTYPE html>
<html>
<body>
<p id="eg"></p>
<script type="text/javascript">
document.getElementById("eg").innerHTML = "Hello world";
</script>
</body>
</html>

EXAMPLE 3
<!DOCTYPE html>
<html>
<head>
<title>Example User Interaction Functions</title>
</head>
<body>
<script type="text/javascript">
document.write("example of alert, confirm and prompt functions :");
 alert("thanks for visiting my site.");
confirm (" do you want to delete the file ");
 prompt (" enter your name: ");
</script>
</body>
</html>

EXAMPLE 4

<!DOCTYPE html>
<html>
<head>
<title>Example of Comments </title>
</head>
<body>
<script type="text/javascript">
document.write(" Single line comments "); / This is an exmple of single line comment
document.write("<BR>");
document.write("Multi line comments "); /* This is an exmple of multi line comment*/
</script>
</body>
</html>

EXAMPLE 5


<!DOCTYPE html>
<html>
<head> <title>Example of Variables </title>
</head>
<body>
<script language="javascript" type="text/javascript">
var x=100;
document.write(" The value of x ="+x)
</script>
</body>
</html>

EXAMPLE 6

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var a = 25;
var b=10;
var result; document.write(" a=25 <BR> b = 10 ");
result=a+b;
document.write(" <BR> a + b = "+result);
result-a-b;
document.write(" <BR> a- b = "+result);
 result=a*b;
document.write(" <BR> a * b = "+result);
result=a/b;
document.write(" <BR> a/b = "+result);
result=a%b;
document.write(" <BR> a % b = "+result);
a++;
document.write(" <BR> Example of a++ = "+a);
b--;
document.write(" <BR> Example of b--= "+b);
</script>
</body>
</html>

EXAMPLE 7

<!DOCTYPE html>
<html>
<body>
<p>Examples of Comparation Operators <p>
 <script type="text/javascript">
var a = 1 ;
var b = 2
document.write(" a = 1 < br > b =2);
document.write(" < br > a <b:") ;
document.write (a < b);
document.write ("< br > a <= b") ;
document.write (a <= b) ;
document.write("< br > a >b:>")
document.write (a > b) ;
document.write(" < br > a >= b a >=b:")

document.write(a>=b);
document.write(" <br> a==b :");
document.write(a==b);
document.write(" <br> a!=b :"); document.write(a!=b);
</script>
</body>
</html>

EXAMPLE 8

<!DOCTYPE html>
<html>
<body> <p>Example of Logical Operators</p>
<script type="text/javascript">
var a = true;
var b= false;
document.write("<br>a=true <br>b=false: ");
document.write(" <br>a && b: ");
document.write(a&&b);
document.write(" <br>a || b: ");
document.write(a||b);
document.write("<br> !a :");
document.write(!a);
document.write("<br> !b : ");
document.write(!b);
</script>
</body>
</html>

EXAMPLE 9

<!DOCTYPE html>
<html>
<html>
<body>
<script type="text/javascript">
var a = 10;
document.write("<br>Value of a =>"+a);
a+=5;
document.write("<br>Value of (a += 5) =>"+a);
a-=5;
document.write("<br>Value of (a -= 5)=>"+a);
a*=5;
document.write("<br>Value of (a *= 5) =>"+a);
a/=5;
document.write("<br>Value of (a /= 5) =>"+a);
a%=5;
document.write("<br>Value of (a %= 5) =>"+a);
</script>
</body>
</html>


EXAMPLE 10

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var a = 10;
var b = 20;
var max;
document.write ("<br>a=10<br>b=20");
max=(a> b) ? 10:20;
document.write ("<br>The maximum number is= ");
document.write(max);
</script>
</body>
</html>


EXAMPLE 11

<!DOCTYPE html>
<html>
<body>
<h2> Example of Local Functions: Area of Rectangle</h2>
<script type="text/javascript">
function area(l, b)
{
return 1 * b;
}
var a = area(5,4);
document.write("<br> Length=5<br>Breath=4<br><br>Area="+a);
</script>
</body>
</html>


EXAMPLE 12

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function area(l, b)
{
 return l* b;
}
</script>
</head>
<body>
<h2> Example of Global Functions: Area of Rectangle</h2>
<script type="text/javascript">
var a=area(5, 4);
document.write("<br> Length=5<br>Breath=4<br><br>Area="+a);
</script>
</body>
</html>



EXAMPLE 13

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var n=prompt("enter a number =");
if(n%2==1)
document.write("number is odd "+ n);
else
document.write("number is even " + n);
</script>
</body>
</html>



EXAMPLE 14

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var a=1;
var b=2
var c=3;
document.write("<br>a=1<br>b=2<br>c=3<br>");
if(a>b && a>c)
document.write("Greater Number is="+a);
else if (b>c)
document.write("Greater Number is="+b);
else
document.write("Greater Number is="+c);
</script>
</body>
</html>



EXAMPLE 15

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var day=prompt("Enter a Number =");
 switch(day)
{
case "1":document.write("<br>Sunday ");
break;
case "2":document.write("<br>Monday ");
break;
case "3":document.write("<br>Tuesday ");
break;
case "4":document.write("<br>Wednesday ");
break;
case "5":document.write("<br>Thursday");
break;
case "6":document.write("<br>Friday ");
break;
case "7":document.write("<br>Saturday");
break;
default: document.write("<br>Invalid Day");
}
</script>
</body>
</html>




EXAMPLE 16

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var counter=1;
while(counter<=10)
{
document.write("<br>"+counter);
counter++;
}
</script>
</body>
</html>

EXAMPLE 17

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var count=1;
var m;
do
{
m=count*5
document.write("<br>"+m);
count++;
}
while(count<=10);
</script>
</body>
</html>

EXAMPLE 18


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var count=1;
var f=1;
var i,n;
n=prompt("Enter a number to calculate =")
for(i=1;i<=n;i++)
f=f*i;
document.write("<br>The factorial is ="+f);
</script>
</body>
</html>

EXAMPLE 19

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object</h2>
<script type="text/javascript">
var car = {
make: "Toyota",
model: "RAV4",
year:2022,
Number:"BA20PA-2020
};
document.write(car.make+"-- "+car.model+"--"+car.year+"--"+car.Number);
</script>
</body>
</html>

Example 20

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object</h2>
<script>
var car = {
make: "Toyota",
model: "RAV4",
year:2022,
number:"BA20PA-2020″,
display: function(){
return this.make+"--"+this.model +"--"+this.year+"--"+this.number;
}
};
document.write(car.display());
</script>
</body>
</html>

Example 21

<!DOCTYPE html>

<html>
<head>
<script type="text/javascript">
function test()
{
document.write ("Example of Event ");
}
</script></head>
<body>
<p> Click On Button to Trigger The Function</p>
<input type="button" onclick="test()" value="Click">
</body>
</html>

Example 22

<!DOCTYPE HTML>
<html>
<head>
<script type ="text/javascript">
var area=new Function("L","B","return L*B")
function display()
{
var result;
result=area(10,20);
document.write("result");
}
</script>
</head>
<body>
</form>
<input type="button"onclick="display()"value="call function">
</form>
</body>
</html>

Example 23

<!DOCTYPE HTML>
<html>
<head>
<script type ="text/javascript">
var area = function( L,B)
{ return L*B;
};
function display()
{var result;
result=area(10,20);
document.write(result);
}
</script>
</head>
<body>
<form>
<input type ="button"onclick="display()"value="call function">
</form>
</body>
</html>

Example 24

<!DOCTYPE HTML>
<html><body>
<h2>java script object</h2>
<script type ="text/javascript">
var a= new array(5);
a=[10,20,30,40,50];
document.write(a);
</script>
<body>
</html>

EXAMPLE 25

<!DOCTYPE html>
<html>
<head>
<title>String Length Property</title>
</head>
<body>
<script type="text/javascript">
var str = new String("This is a sample string" );
document.write("String lenght is" + str.length);
</script>
</body>
</html>

EXAMPLE 26

<!DOCTYPE html>
<html>
<body>
<p>Click Button to Display Image</p>
<button onclick="myfunction()">Click</button>
<script type="text/javascript">
function myfunction()
{var x = document.createElement("IMG");
x.setAttribute("src", "a.jpg");
 x.setAttribute("width", "200");
x.setAttribute("height", "200");
x.setAttribute("alt", "Refress to show image");
 document.body.appendChild(x);
}
</script> </body>
</html>

EXAMPLE 27

<!DOCTYPE html>
<html>
<body>
<img src="pic1.jpg" alt="First Image" width="200" height="200">
<img src="pic2.jpg" alt="Second Image" width="200" height="200">
<img src="pic3.jpg" alt="Third Image" width="200" height="200">
<p>Click Button to display the number of images</p>
<button onclick="testFunction();">Count Image</button>
<p id="test"></p>
<script type="text/javascript">
function testFunction()
{var a=document.images.length;
document.getElementById("test").innerHTML=a;
}
 </script> </body>
</html>

EXAMPLE 28

<!DOCTYPE html>
<html>
<body>
<h2>Type Text on the Textbox</h2>
<input type="text" id="text1" value="">
<button onclick="fun1()">Click</button>
<p id="p1"></p>
<script type="text/javascript">
function fun1()
{var x = document.getElementById("text1").value;
 document.getElementById("p1").innerHTML = x;
}
</script>
</body>
</html>

EXAMPLE 29

<!DOCTYPE html>
<html>
<body>
<h2>SELECT Options</h2>
<select id="s1" size="1">
<option>Coke </option>
<option>Sprite</option>
<option>Fanta</option>
<option>Cola</option>
</select>
<br><br>
<button onclick="fun1()">Try it</button>
<p id="demo"></p>
<script type="text/javascript">
 function fun1()
{var x = document.getElementById("s1").value;
document.getElementById("demo").innerHTML = x;
}
</script> </body>
</html>

EXAMPLE 30

<!DOCTYPE html>
<html>
<h2>Type Text on Textarea</h2>
<br>
<textarea id="t1">
</textarea>
<button type="button" onclick="fun1()">Click</button>
<p id="p1"></p>
<script>
function fun1()
{var x = document.getElementById("t1").value;
document.getElementById("p1").innerHTML = x;
}
</script>
</body>
</html>

EXAMPLE  31

<!DOCTYPE html>
<html>
<body>
<p>Create New Button</p>
<button onclick="fun1()">Click</button>
<script type="text/javascript">
function fun1()
{var = document.createElement("BUTTON");
var t = document.createTextNode("Click Me");
 x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>

EXAMPLE 32

<html>
<head>
<script type="text/javascript">
function validate()
{ if(document.myForm.Name.value == "")
alert("Please provide your name!" ); document.myForm.Name.focus(); return false;
}
if( document.myForm.EMail.value == "")
{ alert("Please provide your Email!");
document.myForm.EMail.focus();
validateEmail();
return false;
if(document.myForm.Zip.value == ""|| isNaN(document.myForm.Zip.value) ||
 document.myForm.Zip.value.length != 5)
alert("Enter zip in the format ##### document.myForm.Zip.focus():
return false;
if (document.my Form.Country.value")
{
alert("Please provide your country!"):
return false;
}
return( true);
</script>
</head>
<body>
<form action="example.php" name="myForm" onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">Name</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr> <td align="right">EMail</td>
<td><input type="text" name="EMail" /></td>
</tr>
<tr> <td align="right">Zip Code</td>
<td><input type="text" name="Zip" /></td>
</tr>
<tr>
<td align="right">Country</td>
<td><select name="Country">
<option value="-1" selected>Choose Country </option>
<option value="1">USA</option>
<option value="2">UK</option>
 <option value="3">Nepal</option>
</select>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

EXAMPLE 33

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>Simple Example of jQuery </h2>
<p>This is a sample paragraph</p>
<button>Click me</button>
</body>
</html>

Example 34

 <HTML>

<body>

<h1>

my first PHP page </h1>

<?php

echo "hello!";

?>

</body>

</html>




Post a Comment

1 Comments