CSS_ESE[1]
CSS_ESE[1]
CSS_ESE[1]
Code:
<html>
<script>
let a = parseInt(prompt("Enter the value of A"));
let b = parseInt(prompt("Enter the value of B"));
document.write("A=",a,"<br>");
document.write("B=",b,"<br>");
document.write("Addition is ",a+b,"<br>");
document.write("Subtraction is ",a-b,"<br>");
document.write("Multiplication is ",a*b,"<br>");
document.write("Division is ",a/b,"<br>");
</script>
</html>
Output:
1. If Else
Code:
<html>
<script>
let a =10;
let b= 20;
if(a==10)
{
document.write("A is 10");
}
else
{
document.write("A is not 10");
}
</script>
</html>
Output:
2. Switch Case:
Code:
<html>
<script>
let a = parseInt(prompt("Enter the day of week"));
switch(a)
{
case 1:document.write("Sunday");
break;
case 2:document.write("Monday");
break;
case 3:document.write("Tuesday");
break;
case 4:document.write("Wednesday");
break;
case 5:document.write("Thursday");
break;
case 6:document.write("Friday");
break;
case 7:document.write("Saturday");
break;
default: document.write("Invalid Input");
break;
}
</script>
</html>
Output:
1. For Loop:
Code:
<html>
<script>
let n =10;
for(i=1;i<=n;i++){
document.write(i,"<br>");
}
</script>
</html>
Output:
2. While Loop:
Code:
<html>
<script>
let n =10;
let i =1;
while(i<=n){
document.write(i,"<br>");
i++;
}
</script>
</html>
Output:
3. Do While Loop:
Code:
<html>
<script>
let n =10;
let i =1;
do{
document.write(i,"<br>");
i++;
}
while(i<=n)
</script>
</html>
Output:
Code:
<html>
<head>
<title>Array Demo</title>
</head>
<body>
<script>
var a=new Array(5);
for(i=1;i<10;i++){
a[i]=i;
document.write(a[i]+"<br>");
}
document.write(" find() returns element with condition: ",a.find((element => element >
7)) +"<br>");
document.write(" findIndex() returns index with condition: ",a.findIndex((element =>
element > 7)) +"<br>");
document.write("indexOf() returns first match index: ",a.indexOf(3) +"<br>");
document.write("lastIndexOf() returns last match index: ",a.lastIndexOf(3) +"<br>");
document.write("pop() removes and returns the last element: ",a.pop() +"<br>"+"<br>");
document.write("push() adds one or more elements at end : ",a.push(10, 11) +"<br>");
document.write("reverse() reverses the elements: ",a.reverse() +"<br>");
document.write("sort() sorted order array: ",a.sort((a, b) => a - b) +"<br>");
</script>
</body>
</html>
Output:
1. JavaScript Function with no Arguments
Code:
<html>
<body>
<script>
function msg()
{
alert("hello! this is message");
}
</script>
<input type="button" onclick="msg()" value="call function"/>
</body>
</html>
Output:
document.write(a);
document.write("<br>");
document.write(b);
document.write("<br>");
document.write(c);
document.write("<br>");
document.write(d);
document.write("<br>");
<form >
<b><h3><p>Radio Button</p></h3></b>
<label>Gender:</label>
<input type="radio" name="gender" value="male" > Male
<input type="radio" name="gender" value="female"> Female<br>
</form>
</body>
</html>
Output:
Mouse Events:
Code:
<!DOCTYPE html>
<head>
<title>Mouse event Code</title>
<style>
#mouseArea{
width:300px;
height:200px;
background-color:lightblue;
border:2px solid blue;
margin-top:50 px;
text-align:center;
line-height:200px;
font-size:20px;
user-select:none;
}
</style>
</head>
<body onload="myfunc()">
<div id="mouseArea">Hover or Click Me!</div>
<script>
function myfunc(){
alert("Page is loaded");
}
const mouseArea=document.getElementById('mouseArea');
mouseArea.addEventListener('mouseover',function(){
mouseArea.textContent="Mouse Over";
mouseArea.style.background='lightgreen';
});
mouseArea.addEventListener('mouseout',function(){
mouseArea.textContent="Hover or Click Me!";
mouseArea.style.backgroundColor='lightblue';
});
mouseArea.addEventListener('mousedown',function(){
mouseArea.textContent="MouseDown";
mouseArea.style.backgroundColor='yellow';
});
mouseArea.addEventListener('mouseup',function(){
mouseArea.textContent="MouseUp";
mouseArea.style.backgroundColor='red';
});
mouseArea.addEventListener('click',function(){
mouseArea.textContent="MouseClicked!";
mouseArea.style.backgroundColor='pink';
});
mouseArea.addEventListener('dblclick',function(){
mouseArea.textContent="Double Click";
mouseArea.style.backgroundColor='white';
});
mouseArea.addEventListener('contextmenu',function(event){
event.preventDefault();
mouseArea.textContent="RightClick!";
mouseArea.style.backgroundColor='orange';
});
</script>
</body>
</html>
Output:
Load Events:
Code:
<html>
<head>
<script>
// Load Event Functions
function onLoadFunc() {
alert("Page is loaded");
}
function onUnloadFunc() {
alert("Page is unloading");
}
function onBlurFunc() {
var fnameInput = document.getElementById("fname");
fnameInput.value = fnameInput.value.toUpperCase();
}
function onResetFunc() {
alert("The form has been reset.");
}
function confirmInput(event) {
event.preventDefault(); // Prevent form submission
var fname = document.getElementById("fname").value;
alert("Hello " + fname + "! You will now be redirected to My Page.");
// Optionally redirect here
// window.location.href = "your_redirect_url";
}
</script>
</head>
<body onload="onLoadFunc()" onunload="onUnloadFunc()">
<h2>Hello</h2>
<form onreset="onResetFunc()" onsubmit="confirmInput(event)">
<label for="fname">Enter your name:</label>
<input id="fname" type="text" size="20" onfocus="onFocusFunc(this)"
onblur="onBlurFunc()" onkeyup="onKeyUpFunc()">
<input type="reset" value="Reset">
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
1. Intrinsic Functions
Code:
<!DOCTYPE html>
<html>
<body>
<form name="myform">
Roll Number:<input type=”text” name=”roll”/>
<br> <br>
Name :<input type=”text” name=”name”/>
<br><br>
<img src ="submit.jpg" onclick="javascript:document.forms.myform.submit()"/>
<br><br>
</form>
</body>
</html>
Output:
2. Disabling Elements
Code:
<html>
<head>
<title>Intrinsic function</title>
<script>
function EnableFunction()
{
document.forms.myform.name.disabled=false
}
function DisableFunction()
{
document.forms.myform.name.disabled=true
}
</script>
</head>
<body>
<form name="myform">
Username:<input type="text" name="name"/>
<br> <br>
<button type="button" onclick="DisableFunction()"> Disable Name Field </button>
<br><br>
<button type="button" onclick="EnableFunction()"> Enable Name Field </button>
</form>
</body>
</html>
Output:
3. Read-Only Elements
Code:
<html>
<head>
<title>Read Only Function</title>
</head>
<body>
<form name="myform">
Username: <input type="text" value="CSS" name="name" readonly/>
</form>
read only
</body>
</html>
Output:
Creating and Reading a Cookie
Code:
createcookie.html :
<html>
<head>
<title>Read and Write cookies</title>
</head>
<body>
<form name="myform" action=" ">
Enter your Name: <input type="text" name="person" /><br>
<input name="Reset" value="Set cookie" type="button" onclick="writecookie()"/>
<input name="Reset" value="Get cookie" type="button" onclick="readcookie()"/>
</form>
<script src="cookie.js"></script>
</body>
</html>
cookie.js :
function writecookie()
{
with(document.myform)
{
document.cookie="Name="+person.value+ ";"
alert("Cookie is Written")
}
}
function readcookie()
{
var x;
if (document.cookie == " "){
x = " ";
}
else{
x=document.cookie;
document.write(x);
}
}
Output:
Code:
<html>
<head>
<script>
function createWindows()
{
for(i=0; i<5; i++)
{
var mywin = window.open("","win"+i,"width=100,height=100");
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Create Windows" onclick="createWindows()"/>
</form>
</body>
</html>
Output:
Code:
<!DOCTYPE html>
<html>
<body>
<form id="loginForm">
<input type="text" id="userId" placeholder="User ID">
<div id="userIdError"></div>
<button type="submit">Login</button>
</form>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
// Validate User ID
if (!userIdRegex.test(userId)) {
document.getElementById('userIdError').textContent =
'User ID must be 4-12 characters, alphanumeric';
}
// Validate Password
if (!passwordRegex.test(password)) {
document.getElementById('passwordError').textContent =
'Password must be 8+ chars, include 1 uppercase and 1 number';
}
function ShowCover(book) {
document.getElementById('DisplayBook').src = MyBooks[book];
}
</script>
</head>
<body>
<p align="center">
<img src="https://m.media-
amazon.com/images/I/81G9qjejtvL._AC_UF1000,1000_QL80_.jpg"
id="DisplayBook" width="200"/>
</p>
<center>
<table border="0">
<tr>
<td align="center">
<a onmouseover="ShowCover(0)" href="#"><b>Visual Basic 2010 Made
Easy</b></a><br>
<a onmouseover="ShowCover(1)" href="#"><b>Visual Basic 2008 Made
Easy</b></a><br>
<a onmouseover="ShowCover(2)" href="#"><b>Visual Basic 6 Made
Easy</b></a><br>
</td>
</tr>
</table>
</center>
</body>
</html>
Output:
1. Menus:
Code:
<html>
<body>
<select>
<option value = "volvo">Volvo</option>
<option value = "saab">Saab</option>
<option value = "opel">Opel</option>
<option value = "audi">Audi</option>
</select>
</body>
</html>
Output:
Code:
<!DOCTYPE html>
<html>
<body>
<script>
</script>
</body>
</html>
Output:
2. Protecting webpage
Code:
<html>
<head>
<script language="JavaScript">
function function2() {
alert("This image is copyrighted")
</script>
</head>
<body oncontextmenu="function2()">
<img oncontextmenu="function2()"
src="https://m.media-amazon.com/images/I/71GozChpIwL._AC_UF1000,1000_QL80_.jpg"
alt="www.java2s.com"
width="99"
height="76">
</body>
</html>
Output:
1. Creating Rotating banner ads
Code:
<html>
<head>
<script language="Javascript">MyBanners=new
Array('https://www.w3schools.com/w3images/fjords.jpg',
'https://www.w3schools.com/w3images/lights.jpg',
'https://www.w3schools.com/w3images/nature.jpg',
'https://www.w3schools.com/w3images/mountains.jpg')
banner=0
function ShowBanners()
(if (document.images)
{banner++
if (banner==MyBanners.length) {
banner=0}
document.ChangeBanner.src=MyBanners[banner]
setTimeout("Show Banners()", 5000)
}
}
</script>
<body onload="ShowBanners()">
<center>
<img src="https://www.w3schools.com/w3images/fjords.jpg" width="900"
height="120" name="ChangeBanner"/>
</center>
</body>
</html>
Output:
2. Slideshow:
Code:
<html>
<head>
<script language="Javascript">
MySlides=new Array('https://www.w3schools.com/w3images/fjords.jpg',
'https://www.w3schools.com/w3images/lights.jpg',
'https://www.w3schools.com/w3images/mountains.jpg',
'https://www.w3schools.com/w3images/nature.jpg'
)
Slide=0
function ShowSlides (SlideNumber){
{Slide=Slide+SlideNumber
if(Slide >MySlides.length-1){
Side=0
}
if(Slide<0) {
Slide=MySlides.length-1
}
document.DisplaySlide.src=MySlides[Slide]
}
}
</script>
</head>
<body>
<P align="center"><img src="https://www.w3schools.com/w3images/fjords.jpg"
name="DisplaySlide" width="900" height="120"/>
<p>
<center>
<table border=0>
<tr>
<td align=center>
<input type="button" value="Back" onclick="ShowSlides(-1)">
<input type="button" value="Forward" onclick="ShowSlides(1)">
</td>
</tr>
</table>
</center>
</body>
</html>
Output: