7 Document Object
7 Document Object
EXPLORING COOKIES
A cookie is a small file. containing information, which a server embeds
on a user computer. Ecah the same computer requests for a webpage from a
server, the server refer to the created cookie for diplaying the requested
webpage. The size of a cookie depends on a browser, put it in generally should
not exceed 1k. A cookie is generally used to store the user name and password
information on a computer so that you need not to enter this information each
time you visit a website.
A cookie has several attributes, which contain vital information about
the cookie, such as its name, the domain name to which it belongs, and the
address of the valid path within a domain. Some commonly used attributes of a
cookie that we can use with JavaScript are as follows:
The name attribute
Represents a variable name and the corresponding value to be stored in
a cookie
The expire attributes
Specifies the the time when a cookie is [Link] is expressed as a unix
timestamp
The domain attributes
Represent a domain name to which a cookie is set. For example, if the
value for the valid domain attribute is [Link], a client sends the cookie
information to the web browser every time the client visits the [Link]
website.
3
The path attributes
Identifies sites among various paths in the same domain. Setting
attribute to the server root(/) allows the entire domain to access the information
stores in a cookie.
The secure attributes
Restricts a browser from sending cookie information over unsecured
connections. It allows the cookie to be sent over any type of hyper text transfer
protocol connections. The default value of the security flag attribute is [Link]
the value to 1 follows the cookie to be sent over a secure HTTP connections.
Creating and storing cookies
JavaScript enables you to create your own cookies that store the user's
information. For example, you can create a cookie to store the name of a website
visitor. When the visitor first opens a Web page,he or she has to enter his or her
name. This name will be stored in a cookie. The next time the visitor arrives at
the same page, he or she will get welcome message. For example
function setCookie(c_name,value,exdays)
{
var datadate=new Date();
[Link]([Link]()+exdays);
var v_value=escape(value)+((exdays==null)? "" : ";
expires="+[Link]());
[Link]=v_name + "=" + v_value;
}
cookiename=anything; expires=date
cookiename=anything; expires=sat,1 Dec 2016 [Link] UTC
Reading cookies
JavaScript also allows you to read the information from a cookie. For
example:
function getCookie(v-name)
{
var i,x,y,cookies_arr=[Link](;);
for(i=0;i<cookie_arr.length;i++)
{
x=cookie_arr[i].substr(0,cookies_arr[i].indexof("="));
y=cookie_arr[i].substr(cookies_arr[i].indexof("=")+1);
x=[Link](/^\s+|\s+$/g,"");
if(x==v_name)
{
return unescape(y);
}
5
}
}
6
Displaying the number of image elements in an HTML document
Working with document object properties
The document object provides various properties that are related to an
HTML document like the TITLE property or the URL
property.
Let's create a Web Page named [Link] to
understand the use of document object properties.
<HTML>
<HEAD>
<TITLE>DOCUMENT OBJECT PROPERTIES</TITLE>
</HEAD>
<BODY>
<H1>Using the properties of the document object</H1>
Following is the URL associated with this document:
<SCRIPT type="text/JavaScript"> [Link]([Link]);
7
</SCRIPT><BR/>
Loading Status:
<SCRIPT type="text/JavaScript"> [Link]([Link]);
</SCRIPT><BR/>
The title of the document is:
<SCRIPT type="text/JavaScript"> [Link]([Link]);
</SCRIPT><BR/>
</BODY>
</HTML>
In the above code the title, url and readystate properties are used. The
URL property shows the URL of opened HTML
document. The readyState property shows the loading status of the
document,and the TITLE property shows the title of the document.
8
function getElement()
{
newwin=[Link]("")
[Link]()
var abc=[Link]("nm")
[Link]("your name length is: "+[Link])
[Link]()
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Using the methods of the document object</H1>
<B>Enter first name:</B><input name="nm" "type="text" size="15"><BR/>
<B>Enter second name:</B><input name="nm" "type="text"
size="15"><BR/>
<B>Enter third name:</B><input name="nm" "type="text" size="15"><BR/>
<INPUT type="button" onclick="getElement()" value="Name length">
</BODY>
</HTML>
In the above code the [Link]() method is used to open a
[Link] [Link]() method opens a document stream. The
getElementsByName() method is used to close the document stream.
9
Displaying text boxes for accepting the name from the user
When you click the name length button after entering names in the
provided fields, a new window opens, displaying the number of names.
10
<HTML>
<HEAD>
<TITLE>Online Shopping</TITLE>
<SCRIPT type="text/JavaScript">
<!--
function order()
{
var order="Dear"
order = order+ [Link][0].[Link]
order = order+ " ,you have ordered an item with size"
if([Link][0].[Link][0].selected)
{
order = order + "small"
}
if([Link][0].[Link][1].selected)
{
order = order+ "medium"
}
if([Link][0].[Link][2].selected)
{
order = order+ "large"
}
order = order+ "and it will be delivered in a"
if([Link][0].packaging[0].checked)
{
order = order+ "cardboard box"
}
if([Link][0].packaging[1].checked)
{
order = order+ "styrofoam-insulated container"
11
}
order = order+ "to the following address:"
order = order+ [Link][0].[Link]
alert(order)
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H1>Online Shopping</H1>
<FORM action="[Link] method="post" onSubmit="order()"
enctype="text/plain">
NAME:
<INPUT name = "NAME" size=20><BR>
Address:
<INPUT name = "Address" size=20><P>
Item size:<BR>
Size:
<SELECT name="size">
<OPTION>Small
<OPTION>Medium
<OPTION>Large
</SELECT><BR>
packaging:<BR/>
<INPUT type=radio name="packaging">Cardboard box<BR>
<INPUT type=radio name="packaging">Styrofoam-insulated container<P>
<INPUT type=submit value="order">
</FORM>
</BODY>
</HTML>
12
In the above code we have created six form elements: two text boxes ,one
combo box, two radio buttons and one button. Next, we have created the order()
function, which displays a message box that shows the current shopping order,
including the items purchased by a customer.
You can see that when you click the order button after filling the
provided fields, you get a JavaScript alert box that displays the delivery details of
an item. On clicking the OK button of the alert box a new mail message window
appears containing the order details as we have entered.
13
Displaying an order item through form elements
Creating cookies
<!DOCTYPE HTML>
<HEAD>
<SCRIPT TYPE="text/javascript">
function getCookie(cname)
{
if([Link]>0)
{
cstart=[Link](cname + "=");
if(cstart!=-1)
{
cstart=cstart+[Link]+1;
14
cend=[Link](";",cstart);
return
unescape([Link](cstart,cend));
}
}
return "";
}
function setCookie(cname,value,expiredays)
{
var exdate=new Date();
[Link]([Link]()+expiredays);
[Link]=cname+ "="
+escape(value)+((expiredays==null) ?
"" : " ;expires="+[Link]());
}
function checkCookie()
{
mycookie=getCookie('mycookie');
if(mycookie!=null && mycookie!="")
{
alert('Welcome again '+mycookie+'! \n The cookies has
already been created.');
}
else
{
mycookie=prompt('please enter your name:',"");
if(mycookie!=null && mycookie!="")
{
setCookie('mycookie',mycookie,30);
}
}
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Creating and Reading cookies</H1>
<P>Click on button to create the cookie: </P>
<INPUT type="button" onclick="checkCookie()" value="Create Cookie">
</BODY>
</HTML>
16
Deleting cookies
You can also reset the value of a cookie or delete the existing cookies
using JavaScript. Let’s create a web page named [Link] to learn how to
reset or delete the cookies. below code is for the [Link] file below code
is Showing the Code for the [Link] File.
<!doctype html>
<head>
<title>Resetting cookie</title>
<script type="text/javascript">
function getCookie(cname)
{
if([Link]>0)
{
cstart=[Link](cname +
"=");
if(cstart!=-1)
{
cstart=cstart + [Link]+1;
cend=[Link](";",cstart)
if(cend==-1)
cend=[Link];
return
unescape([Link](cstring,cend));
}
}
return "";
}
17
function setCookie(cname,value,expiredays)
{
var exdate=new Date();
[Link]([Link]()+expiredays);
[Link]=cname+ "="
+escape(value)+((expiredays==null) ?
"" : ":expires=+[Link]();
}
function checkCookie()
{
mycookie=getCookie('mycookie');
if(mycookie!=null && mycookie!="")
{
alert('welcome again ');
}
else
{
mycookie=prompt('please enter ur
name:',"");
if (mycookie!=null && mycookie!="")
{
setCookie('mycookie',mycookie,30);
}
}
}
function eraseCookie()
{
setCookie('mycookie',"",-1);
alert(' sucessfully erased');
18
}
</script>
</head>
<body>
<h1>reading and creating cookies</h1>
<p>click on button to create: </p>
<input type="button" onclick="checkCookie()" value="Create cookie">
<p>click on the delete cookie button to delete the cookie: </p>
<input type="button" onclick="eraseCookie()" value="delete cookie">
</body>
</HTML>
In addition, we have created a function, named eraseCookie(), which sets the
expiry date of the existing cookie to -1. Next, we have used the Delete cookie
button to call the eraseCookies() function. This function opens an alert box,
displaying a message, The cookie has been successfully deleted. The output is
shown below:
19
you to enter your name, Now, enter your name in the text box of the input box
and click the OK button to create the cookie. After creating the cookie when you
click the Create Cookie button, it displays a message that a cookie has already
been created, as shown below:
20