YBS Unit IV Cookies & Browser Data (1)
YBS Unit IV Cookies & Browser Data (1)
DATA
MR. Y. B. SANAP
LECTURER IN IT DEPT.,
GOVERNMENT POLYTECHNIC, AMBAD
Cookies- Basic Of Cookies
2
Cookies is one of the very efficient way of maintaining state on the client side
Cookie is stored on client’s machine as a text file
When the user visits again, the website reads the data from the cookie.
A cookie is a small text file that lets you store a small amount of data on
the user's computer.
They are typically used for keeping track of information such as user
preferences that the site can retrieve to personalize the page when user
visits the website next time.
Cookies are an old client-side storage mechanism that was originally
designed for use by server-side scripting languages.
Cookies can also be created, accessed, and modified directly using JavaScript,
but the process is little bit complicated and messy.
Cookies- Basic Of Cookies
3
Cookies- Basic Of Cookies
4
Information will get saved once you login, next time cookies data will be used
till the session get closed
Cookies- Basic Of Cookies
5
Name value
How to create cookie in JavaScript
6
In JavaScript, we can create, read, update and delete a cookie by using
document.cookie property
Syntax:
document.cookie=“name=value”;
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.cookie = "username=geeky";
alert(document.cookie);
</script>
</body>
</html>
//example to set and get a cookie
<!DOCTYPE html>
<html>
<head> <title>Example Cookies</title> </head>
<body>
<script>
function setCookie()
{
document.cookie = "collegename=gpambad";
}
function getCookie()
{
if(document.cookie!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie is not set");
//display the cookie's name-value pair separately
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin";
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
Types of Cookies
10
Types of Cookies
1. Session Cookies − These cookies are temporary which are
erased when the user closes the browser. Even if the user logs
in again, a new cookie for that session is created.
2. Persistent cookies − These cookies remain on the hard disk
drive unless user wipes them off or they expire. The Cookie's
expiry is dependent on how long they can last.
JavaScript can create, read or delete a cookies using
document.cookie property.
How to create cookie in JavaScript
11
When we work with cookie client and server is involved.
To run the cookie we need server
Cookies- Basic Of Cookies/Cookie Attributes
12
</body>
</html>
Cookie max-age attribute
15
If I want to active only for 1 minute ..we will write 60 second
</body>
</html>
Cookie path attribute
18
</body>
</html>
Cookie Domain attribute
20
</body>
</html>
<html>
<body>
<script>
function openWindow() {
window.open('https://www.javatpoint.com');
}
</script>
<body>
<b> Click the button to open new window in same tab </b>
<br><br>
<button onclick="openWindow()"> Open Window </button>
</body>
</html>
// we need to provide any name to the new window and write some text
into //it. This name will pass to the window.open() method
<html>
<script>
function openWindow() {
var newtab =window.open("","anotherWindow","width=300,height=150");
newtab.document.write("<p>This is 'anotherWindow'. It is 300px wide
and 150px tall new window! </p>");
}
</script>
<body>
<b> Click the button to open the new user-defined sized window </b>
<br><br>
<button onclick="openWindow()">Open Window</button>
</body>
</html>
JavaScript Window close method
33
JavaScript provides an in-built function named close() to close the
browser window that is opened by using window.open() method.
Unlike the window.open() method, it does not contain any
parameter. This window.close() method simply close the window
or tab opened by the window.open() method.
Remember that - You have to define a global JavaScript
variable to hold the value returned by window.open() method,
which will be used later by the close() method to close that
opened window.
Syntax: window.close() //this method does not have any parameter
Here, window is the name of that window that is opened by the
window open method.
Close the window opened by window.open()
34
In this example, we will show you how to close the window or tab
opened by the window.open() method. Firstly, we will open a
website URL in a new window (size defined in code) using a
button click and then use another button to close that opened
window. See the below code how it will be done:
<html>
<script>
var newWindow;
function openWindow() {
newWindow = window.open("", "myWindow", "width=200,height=100");
newWindow.document.write("<p>It is my 'newWindow'</p>");
}
function closeWindow() {
newWindow.close();
}
</script>
<h3 style="color:brown"> Close Window Example </h3>
<body>
<button onclick="openWindow()">Open New Window</button>
<br><br>
<button onclick="closeWindow()">Close New Window </button>
</body>
</html>
<html>
<head>
<title> Open and close window method example </title>
<script>
var newWindow;
// function to open the new window tab with specified size
function windowOpen() {
newWindow = window.open(
"https://www.javatpoint.com/", "_blank", "width=500, height=350");
}
<center>
<h2 style="color:green"> Window open() and close() method </h2>
<body>
<b> Click the button to open Javatpoint tutorial site </b><br>
<button onclick="windowOpen()"> Open Javatpoint </button>
<br><br>
<b> Click the button to close Javatpoint tutorial site </b><br>
<button onclick="windowClose()"> Close Javatpoint </button>
</body>
</center>
</html>
<html>
<head>
<title>JavaScript New Window Example</title>
</head>
<script type="text/javascript">
function poponload()
{
testwindow = window.open("https://www.google.com/", "mywindow",
"location=1,status=1,scrollbars=1,width=300,height=400");
testwindow.moveTo(0, 0);
}
</script>
<body onload="javascript: poponload()">
<h1>JavaScript New Window Example</h1>
</body>
</html>
Giving The New Window Focus
38
We can set the desired position for the window. Using the left &
top attributes values the window position can be set.
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function new_win()
{
window.open("http://
www.google.com","mywin","width=400,height=300,
screenX=50,left=50,screenY=50,top=50");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform">
<INPUT TYPE="button" value="Open New Window" onClick="new_win()">
</FORM>
</BODY>
</HTML>
Changing The Content Of Window
42
By writing some text to the newly created window we can change
the contents of a window.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new window with Changing the content....</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>Welcome to CSS... By Current
Window...</p>");
}
</script>
</body>
</html>
Closing A Window
44
<body>
<h1>The Window Object</h1>
<h2>The scrollTo() Method</h2>
<script>
function scrollWin() {
window.scrollTo(200, 0);
}
</script>
</body>
</html>
//Scroll the document to the vertical position 500::
//window.scrollTo(0, 500);
<!DOCTYPE html>
<html>
<style>
body {
height: 5000px;
}
</style>
<body>
<h1>The Window Object</h1>
<h2>The scrollTo() Method</h2>
<script>
function scrollWin() {
window.scrollTo(0, 500);
}
</script>
</body>
</html>
Multiple Window At Once
49
We can create a web page using the window object with the help
of write method.
Inside the write() we have to write the content of the web page
with help of the html elements such as <head>,<body>,<h1>.
<HTML> </SCRIPT> </HEAD>
<HEAD> <BODY>
<SCRIPT language="JavaScript"> <FORM name="myform">
<!-- <INPUT TYPE="button" value="Create Web
function new_win() Page" onClick="new_win()">
{ </FORM> </BODY> </HTML>
var mywin=window.open("","mywin","width=400,height=300")
mywin.document.write("<html>");
mywin.document.write("<head>");
mywin.document.write("<title>WEB SITE DEMO</title>");
mywin.document.write("</head>");
mywin.document.write("<body>");
mywin.document.write("<h2>This is a new Web Page</h2>");
mywin.document.write("<h3>Welcome User...!!!!</h2>");
mywin.document.write("</body>");
mywin.document.write("</html>");
}
JavaScript In URLs
53
2. setInterval(function, milliseconds)
Same as setTimeout(), but repeats the execution of the function
continuously.
The first parameter is the function to be executed.
The second parameter indicates the length of the time-interval
between each execution.
<!DOCTYPE html>
<html>
<body>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<script>
var myVar = setInterval(myTimer, 1000);
function myTimer()
{
var d = new Date();
document.getElementById("demo").innerHTML =
d.toLocaleTimeString();
}
</script>
</body>
</html>
<html>
<head>
<title>JavaScript setInterval() Method</title>
<script type="text/javascript">
function timemsg()
{
var si = setInterval("alert('2 seconds!')", 2000);
}
</script>
</head>
<body>
<form>
<input type="button" value="Timed Alert Box"
onClick="timemsg()">
</form> </body> </html>
Browser Location
61
When user opens any website, the browser will send the request
to the server. Then the server sends the requested webpage to the
browser. The web page is stored on server. If we want to know the
location or path of the webpage, we use window.location object. It
is used to find the location and path of the current webpage.
The window.location object is useful for finding out the current location
or path of the web page.
Properties of window.location as follow:
1. window.location.hostname
2. window.location.pathname
3. window.location.protocol
4. window.location.assign
<!DOCTYPE html>
<html>
<body>
<p id="ID"></p>
<script>
document.getElementById("ID").innerHTML= "This web page
is at path: "+window.location.pathname;
</script>
</body>
</html>