06 Sep 2023
06 Sep 2023
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var now = new Date();
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thrusday",
"Friday", "Staturday"];
var months = ["January", "Feb",
"March","April","May","June","July","August"];
function SetClock(){
document.getElementById("date").innerHTML = `
${weekdays[now.getDay()]} ${now.getDate()}, $
{months[now.getMonth()]} ${now.getFullYear()}
`;
}
function SetTimer(){
var now = new Date();
document.getElementById("time").innerHTML= now.toLocaleTimeString();
}
function bodyload(){
SetClock();
setInterval(SetTimer,1000);
var now = new Date();
var hrs = now.getHours();
var status = document.getElementById("status");
var poster = document.getElementById("poster");
Syntax:
<form onsubmit="function" onreset="function"> </form>
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form onsubmit="alert('Form will submit data to server')" onreset="alert('Form
will reset')">
User name : <input type="text" name="UserName">
<button type="submit">Submit</button>
<button type="reset">Cancel</button>
</form>
</body>
</html>
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function CityChange(){
frmMovies.submit();
}
</script>
</head>
<body>
<form name="frmMovies">
<dl>
<dt>User Name</dt>
<dd><input type="text" name="UserName"></dd>
<dt>City</dt>
<dd>
<select onchange="CityChange()" name="City">
<option>Select City</option>
<option>Delhi</option>
<option>Hyd</option>
</select>
</dd>
</dl>
</form>
</body>
</html>
Syntax:
document.querySelector("button").addEventListener("eventName", function(){
// actions to perform
})
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function bodyload(){
document.getElementById("btnInsert").addEventListener("click",
function(){
document.write("Record Inserted");
})
}
</script>
</head>
<body onload="bodyload()">
<button id="btnInsert">Insert</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function ChangeEvent(){
var eventName = document.querySelector("select").value;
document.getElementById("btnInsert").addEventListener(eventName,
function(){
document.write("Record Inserted");
})
}
</script>
</head>
<body>
<div>
<select onchange="ChangeEvent()">
<option>Select Event</option>
<option value="mouseover">Mouse Over</option>
<option value="click">On Click</option>
</select>
</div>
<br><br><br>
<button id="btnInsert">Insert</button>
</body>
</html>
Query String:
- A query string is appended into URL with a key and value reference
?key=value
- Any form can submit its data into query string on "Get" request.
- You can access querystring using
"location.search"
Ex:
1. Add a new folder
"State"
2. Add files
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>Home Page</h2>
<form action="welcome.html">
User Name :
<input type="text" name="UserName">
<button>Submit</button>
</form>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function bodyload(){
var queryString = location.search;
var name = queryString.substring(queryString.indexOf("=")+1);
document.querySelector("p").innerHTML = `Hello ! ${name}`;
}
</script>
</head>
<body onload="bodyload()">
<h2>Welcome Page</h2>
<p></p>
</body>
</html>
Local Storage
- Every browser have a local storage.
- It is permanent storage.
- It is available even after your device shut-down.
- You have to delete manually from browser memory.
- It is accessible across all tabs in browser.
Syntax:
localStorage.setItem("key", "value");
Ex:
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function SubmitClick(){
localStorage.setItem("uname",
document.getElementById("UserName").value);
location.href = "welcome.html";
}
</script>
</head>
<body>
<h2>Home Page</h2>
User Name :
<input type="text" id="UserName" name="UserName">
<button onclick="SubmitClick()">Submit</button>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function bodyload(){
if(localStorage.getItem("uname")==null){
location.href = "home.html";
} else {
document.querySelector("p").innerHTML = `Hello ! $
{localStorage.getItem("uname")}`;
}
}
</script>
</head>
<body onload="bodyload()">
<h2>Welcome Page</h2>
<p></p>
</body>
</html>
Session Storage
- It is temporary storage.
- It is not accessible to other tabs.
- It is removed when browser is closed.
Syntax:
sessionStorage.setItem("key", "value");
var ref = sessionStorage.getItem("key");
sessionStorage.removeItem("key");
Ex:
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function SubmitClick(){
sessionStorage.setItem("uname",
document.getElementById("UserName").value);
location.href = "welcome.html";
}
</script>
</head>
<body>
<h2>Home Page</h2>
User Name :
<input type="text" id="UserName" name="UserName">
<button onclick="SubmitClick()">Submit</button>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function bodyload(){
if(sessionStorage.getItem("uname")==null){
location.href = "home.html";
} else {
document.querySelector("p").innerHTML = `Hello ! $
{sessionStorage.getItem("uname")}`;
}
}
function SignoutClick(){
sessionStorage.removeItem("uname");
location.href = "home.html";
}
</script>
</head>
<body onload="bodyload()">
<h2>Welcome Page <button onclick="SignoutClick()">Signout</button> </h2>
<p></p>
</body>
</html>
Cookies
- Cookies are simple text documents.
- Client related information can be stored in a cookie file and used later by page.
- Cookies need to be enabled in browser.
- You can verify cookie status by using
Syntax:
document.cookie = "key=value; expires= date time";
Ex:
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function SubmitClick(){
document.cookie = `uname=${document.getElementById("UserName").value};
expires=Thu 7, Sep 2023 10:50:00`;
location.href = "welcome.html";
}
</script>
</head>
<body>
<h2>Home Page</h2>
User Name :
<input type="text" id="UserName" name="UserName">
<button onclick="SubmitClick()">Submit</button>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function bodyload(){
document.querySelector("p").innerHTML =
document.cookie.substring(document.cookie.indexOf("=")+1);
}
function SignoutClick(){
sessionStorage.removeItem("uname");
location.href = "home.html";
}
</script>
</head>
<body onload="bodyload()">
<h2>Welcome Page <button onclick="SignoutClick()">Signout</button> </h2>
<p></p>
</body>
</html>