Chapter 4 Cookies & Browser Data
Chapter 4 Cookies & Browser Data
• The simplest way to create a cookie is to assign a string value to the document.cookie
object, which looks like this.
document.cookie = "key1 = value1;expires = date";
• Here the expires attribute is optional. If you provide this attribute with a valid date or
time, then the cookie will expire on a given date or time and thereafter, the cookies' value
will not be accessible.
• document.cookie = "CustomerName=alinka;"
JAVASCRIPT FOR STORING/WRITING COOKIE
<html>
<head>
<title>Write Cookie</title>
<script language="Javascript" type="text/javascript">
function WriteCookie()
{
with (document.form1)
{
document.cookie =“UserName="+ uname.value+";"+“
expires=Thu, 18 Dec 2020 12:00:00 UTC";
alert("Cookie Written")
}
}
</script>
</head>
<body>
<form name=“form1" action="" >
Enter your name:
<input type="text" name=“uname" />
<input type = "button" value = "Set Cookie" onclick = "WriteCookie();"/>
</form>
</body>
</html>
READING COOKIES
• Reading a cookie is just as simple as writing one, because the value of the
document.cookie object is the cookie.
• So you can use this string whenever you want to access the cookie.
• The document.cookie string will keep a list of name=value pairs separated by
semicolons, where name is the name of a cookie and value is its string value .
• You can use strings' split() function to break a string into key and values as follows:-
<html>
<head>
<title>Read Cookie</title>
<script language="Javascript" type="text/javascript">
function ReadCookie()
{
with (document.form2)
{
if (document.cookie == "")
{
text1.value = "No cookies"
}
else
{
text1.value = document.cookie.split('=')[1]
}
}
}
</script>
</head>
<body>
<form name=“form2" action="" >
Cookie: <input type="text" name=“text1" />
<BR>
<INPUT name="Reset“ value="Get Cookie" type="button“ onclick="ReadCookie()"/>
</FORM>
</body>
</html>
SETTING THE EXPIRATION DATE
• You can extend the life of a cookie by setting an expiration date and saving the
expiration date within the cookie.
• The expiration date is typically an increment of the current date.
• For example, you might say that the cookie expires three months from the day the
cookie was created.
• Cookies are automatically deleted when either the browser session ends or its
expiration date has been reached.
• However, you can remove a cookie at any time by setting its expiration date to a date
previous to the current date.
• This forces the browser to delete the cookie
• The most efficient way to reset the expiration date is to use the getDate() method of the
Date variable, then subtract 1 from the date returned by this method, and then assign
the difference to the Date variable
function DeleteCookie()
{
expireDate= new Date() expireDate.setDate(expireDate.getDate()-1)
with (document.form1)
{
var CustomerName = customer.value
document.cookie = "CustomerName1="+ CustomerName+";
expires="+expireDate.toUTCString()
alert("Cookie Deleted")
}
}
BROWSER WINDOWS
➢ Open the Window
➢ Giving the New Window Focus
➢ Placing the Window into Position on the Screen
➢ Changing the Contents of a Window
➢ Closing the Window
➢ Scrolling a Web Page
➢ Opening Multiple Windows at Once
➢ Creating a Web Page in a New Window
➢ JavaScript in URLs
➢ JavaScript security
➢ Timers
➢ Browser location and history.
INTRODUCTION
• In this chapter, you’ll learn how to manipulate the browser window itself using a
JavaScript.
• You can use JavaScript
open a new browser window while your JavaScript is running
determine the size of the window
determine whether or not the window has a toolbar or scroll bar
set up other styles.
you can use JavaScript to change the content of each of them dynamically.
OPEN THE WINDOW
➢ The browser window is an object.
➢ Whenever you want to do something with the browser window, you must reference a
window and then reference the property or method of the window that you want to access.
➢ For example, here’s how to open an empty browser window that uses the default settings:
MyWindow = window.open()
➢ The open() method returns a reference to the new window, which is assigned to the
MyWindow variable.
➢ A window has many properties, such as its width, height, content, and name.
➢ You set these attributes when you create the window by passing them as parameters to the
open() method:
1. The first parameter is the full or relative URL of the web page that will appear in the new
window.
2. The second parameter is the name that you assign to the window.
3. The third parameter is a string that contains the style of the window.
Following table shows a list of styles that you can set.
Window Styles
Settings for parameters:
➢Position:
✓ left/top (numeric) – coordinates of the window top-left corner on the screen. There is a
limitation: a new window cannot be positioned offscreen.
✓ width/height (numeric) – width and height of a new window. There is a limit on minimal
width/height, so it’s impossible to create an invisible window.
➢ Window features:
✓ menubar (yes/no) – shows or hides the browser menu on the new window.
✓ toolbar (yes/no) – shows or hides the browser navigation bar (back, forward, reload etc) on
the new window.
✓ location (yes/no) – shows or hides the URL field in the new window.
✓ status (yes/no) – shows or hides the status bar. Again, most browsers force it to show.
✓ resizable (yes/no) – allows to disable the resize for the new window. Not recommended.
✓ scrollbars (yes/no) – allows to disable the scrollbars for the new window. Not recommended.
EXAMPLE
➢ Let’s say that you want to open a new window that has a height and a width of 300 pixels
and displays an advertisement that is an image. All other styles are turned off.
window.focus( )
<html>
<head>
<title>Giving new window focus</title>
<script language="Javascript" type="text/javascript">
function OpenNewWindow() {
MyWindow = window.open('C:/CSS/nature.jpg','','height=300, width=300');
MyWindow1 = window.open('C:/CSS/nature1.jpeg','', 'height=400, width=400');
MyWindow1.focus();
}
</script>
</head>
<body>
<form action="" method="">
<p>
<input name="OpenWindow" value="Open Window" type="button"
onclick="OpenNewWindow()"/>
</p>
</form>
</body>
</html>
PLACING THE WINDOW INTO POSITION
ON THE SCREEN
• The browser determines the location on the screen where a new window will be displayed.
• you can specify the location by setting the left and top properties of the new window when
you create it.
• The left and top properties create the x and y coordinates, in pixels, of the upper-left
corner of the new window.
• The following example shows how to position a new window in the upper-left corner of the
screen by setting the left property to 0 and the top property to 0 .
<html>
<head>
<title>Position New Window</title>
<script language="Javascript" type="text/javascript">
function OpenNewWindow() {
MyWindow = window.open('C:/CSS/nature.jpg','myAdWin', 'height=300, width=300,left=100,top=100');
}
</script>
</head>
<body>
<form action="" method="">
<p>
<input name="OpenWindow" value="Open Window" type="button" onclick="OpenNewWindow()"/>
</p>
</form>
</body>
</html>
CHANGING THE CONTENTS OF A WINDOW
➢ If we want to change the content of an open window each time and display
something different in the window.
<body>
<html >
<form action="" method="">
<head>
<p>
<title>Changing Content of Window</title>
<input name="JS" value="JS Book"
<script language="Javascript" type="text/javascript">
type="button"
function OpenNewWindow(Ad) {
onclick="OpenNewWindow('C:/CSS/js.png')"/>
MyWindow = window.open(Ad, 'myAdWin',
<input name=" JS1" value="JS Book1"
'status=0,toolbar=0, location=0, menubar=0,
type="button"
directories=0,resizable=0, height=250, width=250')
onclick="OpenNewWindow('C:/CSS/js1.jpg')"/>
}
</p>
</script>
</form>
</head>
</body>
</html>
CLOSING THE WINDOW
window.close()
•
<!DOCTYPE html>
<html>
<body>
<button onclick="openWin()">Open </button>
<button onclick="closeWin()">Close </button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("http://gpsolapur.ac.in/", "myWindow",
"width=500,height=500,left=200,top=200");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
SCROLLING A WEB PAGE
• JavaScript is used to scroll the web page automatically by calling the scrollTo() method of the
window.
• The scrollTo() method requires two parameters, which are the x and y coordinates of the top-
left corner of the viewable area of the web page that you want to display.
• Syntax
window.scrollTo(x, y)
<html>
<head>
<title>Open Multiple Windows</title>
<script language="Javascript" type="text/javascript">
function Launch() {
for (i=0; i < 5;i++)
{
Win = window.open('','win'+i,'width=100,height=100')
}
}
</script>
</head>
<body>
<form action=" " method="">
<p>
<input name="Windows1" value="Multiple Window" type="button" onclick="Launch()"/>
</p>
</form>
</body>
</html>
CREATING A WEB PAGE IN A NEW WINDOW
• You can place dynamic content into a new window by using the document .write() method to
write HTML tags to the new window.
<html>
<head>
<title>Changing Content of Window</title>
<script language="Javascript" type="text/javascript">
function Window() {
MyWindow = window.open('', 'myWin', 'height=250, width=250')
MyWindow.document.write('<html>')
MyWindow.document.write('<head>')
MyWindow.document.write('<title> Writing Content<\/title>')
MyWindow.document.write('<\/head>')
MyWindow.document.write('<body style="background-color:pink">')
MyWindow.document.write('<form action="" method="">')
MyWindow.document.write('<P>')
MyWindow.document.write('Frist Name:<input name="FirstName" type="text" \/>')
MyWindow.document.write('<BR>')
MyWindow.document.write('<INPUT name="submit" type="submit" \/>')
MyWindow.document.write('<\/p>')
MyWindow.document.write('<\/form>')
MyWindow.document.write('<\/body>')
MyWindow.document.write('<\/html>')
MyWindow.focus()
}
</script>
</head>
<body>
<form action="" method="">
<p>
<input name="OpenWindow" value="Open Window" type="button" onclick="Window()"/>
</p>
</form>
</body>
</html>
JAVASCRIPT TIMING EVENTS
➢ The window object allows execution of code at specified time intervals.
➢ These time intervals are called timing events.
The two key methods to use with JavaScript are:
•setTimeout(function, milliseconds)
This Function is commonly used if you wish to have your function called once after the
specified delay( number of milliseconds.)
•setInterval(function, milliseconds)
This function is commonly used to set a delay for functions that are executed again and again,
such as animations
setTimeout() Example
<html>
<body>
<p>Click "Try it". Wait 3 seconds, and the page will alert "Hello".</p>
<button onclick="setTimeout(myFunction, 3000);">Try it</button>
<script>
function myFunction() {
alert('Hello setTimeoutDemo');
}
</script>
</body>
</html>
setInterval() Example
<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>