Professional Documents
Culture Documents
Javascript - Part III: Indian Institute of Technology Kharagpur
Javascript - Part III: Indian Institute of Technology Kharagpur
1
Using Cookies
Introduction
• What is a cookie?
¾A cookie is a small amount of named data
stored by the web browser, and is
associated with a particular web page or
web site.
¾Serves to give the web browser the
capability to memorize something.
Some user preferences, for instance.
CGI scripts on the server can read and write
cookie values that are stored on the client.
2
• Javascript can manipulate cookies using the
“cookie” property of the “document” object.
¾It is a string property that allows one to
read, create, modify, and delete cookies
associated with the current web page.
¾In addition to a name and value, each
cookie has optional attributes:
expires : specifies the cookie’s lifetime.
path : web pages with which it is associated.
secure : a boolean value; if set, cookies are
transmitted using HTTPS (say).
Storing Cookies
3
Reading Cookies
• Basic idea:
¾The “cookie” property returns a string that
contains all the cookies that apply to the
current document.
A list of name=value pairs separated by “;”.
¾To determine the value of a cookie:
Use the String.indexOf() and String.substring()
methods.
Or, use String.split() to break the string into
individual cookies.
• An example:
4
Using Java with Javascript
5
¾Examples:
document.myapplet.x_size = 150;
document.myapplet.redraw();
document.myapplet.drawLine (10,10,80,100);
6
• An example:
import netscape.javascript.*;
public void init()
{
JSObject win = JSObject.getWindow (this);
win.eval (“alert (‘Hello, Good Day!’)”;
// Use eval to run Javascript function
}
7
“window” Object
Introduction
• Basic idea:
¾Primary task of a web browser is to display
HTML documents in a window.
The “document” object represents the HTML
document.
The “window” object represents the window (or
frame) where the document is being displayed.
8
The Object Hierarchy
navigator
Current frames [ ]
Window
location
history
document
screen
9
¾location
represents the URL of the document displayed
in the window.
¾name
the name of the window.
¾frames [ ]
an array of window objects that represents the
frames within the window.
¾parent
if the current window is a frame, a reference to
the frame of the window that contains it.
10
¾moveBy(), moveTo()
moves the window to a new position.
¾resizeBy(), resizeTo()
changes the size of the window.
¾scrollBy(), scrollTo()
scrolls the document displayed in the window.
¾setInterval(), clearInterval()
schedules or cancels a function to be invoked
repeatedly with a specified delay.
¾setTimeout(), clearTimeout()
schedules or cancels a function to be invoked
once after a specified number of milliseconds.
“navigator” Object
11
Introduction
12
“screen” Object
Introduction
13
¾colorDepth
specifies the base-2 logarithm of the number of
colors that can be displayed.
“location” Object
14
Introduction
15
“history” Object
Introduction
16
• Property:
¾length
contains the number of URLs stored in the
browsing history.
• Methods:
¾back()
moves backward in the window’s browsing
history.
¾forward()
moves forward in the window’s browsing
history.
¾go()
takes an integer argument, and can skip
forward or backward in the history by the
specified number of pages.
17
“document” Object
Introduction
18
• Main properties of “document” object:
¾alinkColor, linkColor, vlinkColor
specifies the color of hyperlinks (normal color
of unvisited link, normal color of visited link,
and color of link while it is activated).
¾anchors [ ]
an array of “anchor” objects representing the
anchors in the document.
¾applets [ ]
an array of “applet” objects representing the
Java applets in the document.
¾bgColor, fgColor
represents the background and foreground
colors of the document.
¾cookie
a special property that allows Javascript
programs to read and write HTTP cookies.
¾forms [ ]
an array of “form” objects representing the
<FORM> elements in the document.
¾images [ ]
an array of “image” objects representing the
<IMG> elements in the document.
19
¾lastModified
last modification date of the document.
¾links [ ]
an array of “link” objects representing the
hypertext links in the document.
¾location, URL
specifies the URL from which the doccument
was loaded.
¾title
the text between the <TITLE> and </TITLE>
tags.
20
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 26
21
Quiz Solutions on Lecture 26
22
Quiz Solutions on Lecture 26
QUIZ QUESTIONS ON
LECTURE 27
23
Quiz Questions on Lecture 27
24