Topic 4 CSS
Topic 4 CSS
1 Cookies
Thus the name value pair separated by = sign and terminated by a delimiter like semicolon(;), the cookie can be
assigned to document.cookie. Instead of window.document.cookie we can simply write document.cookie
TM
Technical Publications (4 - 1)
- An up thrust for knowledge
Client Side Scripting Language 4-2 Cookies and Browser
Data
Ex. 4.1.1 Write a JavaScript for creating a cookie for the user name.
Sol. :
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function CreateCookie()
{
with(document.form1)
{
document.cookie="UserName"+username.value+";" alert("Cookie is created");
}
}
</script>
<head>
<body>
<form name="form1">
Enter Name: <input type="text" name="username"/>
<input type="button" value="Create Cookie" onclick="CreateCookie()"/>
</form>
</body>
</html>
Output
TM
Client Side Scripting Language 4-4 Cookies and Browser
Data
Output
TM
Client Side Scripting Language 4-6 Cookies and Browser
Data
Ex. 4.1.4 : Write a JavaScript to set the new expiry date of cookies.
Sol. :
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function SetExpDtCookie()
{
expiryDate=new Date
expiryDate.setMonth(expiryDate.getMonth()+1)
with(document.form1)
{
document.cookie="UserName"+username.value+";"
document.cookie="expires="+expiryDate.toUTCString ()+";"
alert("New expiry Date is set for the Cookie");
}
}
</script>
</head>
<body>
<form name="form1">
Enter name: <input type="text"
name="username"/>
<input type="button" value="Set New Expiry Date" onclick="SetExpDtCookie()"/>
</form>
</body>
</html>
Output
TM
Client Side Scripting Language 4-7 Cookies and Browser
Data
4.2 Browser
It is possible to open a new browser window from a currently running JavaScript. One can determine
the size, location of this window, toolbar, scroll bar or any other style that normally the browser
windows have.
Once the new browser window is set up, it is also possible to change the contents within that window
dynamically.
Syntax
The syntax of using open() method is
window.open(URL, name, syle)
where
URL : The URL specifies the URL of the web page that will appear in new window. This is an optional
parameter.
name : The name that could be assigned to a window. This is an optional parameter
style : The style of the window includes various parameters such as toolbar, scrollbar, location,height and width
of window and so on. This is an optional parameter. Various styles that can be possible are enlisted in the
following table
Style Purpose
left=pixels The left position of the window. Negative values not allowed
top=pixels To determine the top position of the window. Negative values not allowed
TM
Client Side Scripting Language 4-8 Cookies and Browser
Data
Return Value
A newly created window gets opened up on success and null on failure
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Open Window" onclick="OpenWindowFunction()"/>
</form>
</body>
</html>
TM
Client Side Scripting Language 4-9 Cookies and Browser
Data
Output
var mywin=window.open("","MyWindow",
"status=0,toolbar=0,location=0, menubar=0,directories=0,
resizable=0,width=200,height=200")
this.focus()
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Open Window" onclick="OpenWindowFunction()"/>
</form>
</body>
</html>
Client Side Scripting Language 4 - 10 Cookies and Browser
Data
Output
JavaScript Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function OpenWindowFunction()
{
Var mywin=window.open ("","MyWindow","left=0, top=0,width=200,height=200")
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Open Window" onclick="OpenWindowFunction()"/>
</form>
</body>
</html>
Client Side Scripting Language 4 - 11 Cookies and Browser
Data
Output
Script Explanation : In above JavaScript, as we set the style attributes as left=0, right=0 in open() method,
the window is created and positioned at the extreme left and top location of screen.
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function OpenWindowFunction()
{
var mywin=window.open("","MyWindow","width=200,height=200")
mywin.document.write("<p> This line is written in current window</p>");
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Open Window" onclick="OpenWindowFunction()"/>
Client Side Scripting Language 4 - 12 Cookies and Browser
Data
</form>
</body>
</html>
Output
4.2.5
Closing a Window
The most simple operation about the window is to close it. It can be closed using the function close().
For example -
CloseWinDemo.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var mywin;
function OpenWindowFunction()
{
mywin=window.open("","MyWindow","left=500, top=400,width=200,height=200")
}
function CloseWindowFunction()
{
mywin.close();
}
</script>
</head>
Client Side Scripting Language 4 - 13 Cookies and Browser
Data
<body>
<form name="form1">
<input type="button" value="Open Window" onclick="OpenWindowFunction()"/>
<input type="button" value="Close Window" onclick="CloseWindowFunction()"/>
</form>
</body>
</html>
Output
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function CreateFunction()
{
var mywin=window.open("","MyWindow","width=300,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!!!</h3>");
mywin.document.write("</body>");
mywin.document.write("</html>");
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Create Web Page" onclick="CreateFunction()"/>
</form>
</body>
</html>
Output
Client Side Scripting Language 4 - 16 Cookies and Browser
Data
4.2.11 Timers
Using window object it is possible to execute the code at specified time intervals.
Two functions are supported by window object for handling timing events and those are –
1. setTimeout 2. setInterval
Let’s discuss them
(1) setTimeout
This method executes function after waiting for a specified number of milliseconds.
Syntax
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function MyFunction()
{
alert("Welcome User!!!");
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Click Me" onclick="setTimeout(MyFunction,4000);"/>
</form>
</body>
</html>
Client Side Scripting Language 4 - 18 Cookies and Browser
Data
Output
(2) SetInterval
This function executes the specific function at every give time interval
Syntax
Example
<!DOCTYPE html>
<html>
<body>
<p id="ID"></p>
<script type="text/javascript">
var t=setInterval(MyFunction,2000);
function MyFunction()
{
document.getElementById("ID").innerHTML+="<p>Welcome User!!!</p>";
}
</script>
</body>
</html>
Client Side Scripting Language 4 - 19 Cookies and Browser
Data
Output
Note that the welcome message will be displayed repeatedly on the browser window after some specific time interval.
(1) Location
The window.location object is useful for finding out the current location or path of the web page.
Various properties used by window.location are described in the following table
1. window.location.hostname It returns the name of the host on which the web page is running.
2. window.location.pathname It returns the path name at which the web page is located. It includes
folder and file name.
3. window.location.protocol It returns the web protocol used such as HTTP, File, HTTPS.
Ex. 4.2.2 : Write a JavaScript to display the pathname of the web page using window.location object.
Sol. :
<!DOCTYPE html>
<html>
<body>
<p id="ID"></p>
<script type="text/javascript">
document.getElementById("ID").innerHTML = "This web page is at path: " + window.location.pathname;
</script>
</body>
</html>
Client Side Scripting Language 4 - 20 Cookies and Browser
Data
Output
Ex. 4.2.3 : Write a JavaScript to display the protocol of the web page using window.location object.
Sol. :
<!DOCTYPE html>
<html>
<body>
<p id="ID"></p>
<script type="text/javascript">
document.getElementById("ID").innerHTML = "This web page is using the protocol: " + window.location.protocol;
</script>
</body>
</html>
Output
Client Side Scripting Language 4 - 21 Cookies and Browser
Data
(2) History
The window.history object is used for displaying browser history.
There are two important methods of window.browser
(1) window.history.back() : This method loads the previous URL in the history list. It is like clicking Back
button on Browser window.
(2) window.history.forward() : This method loads the next URL in the history list. It is like clicking
Forward button on Browser window.
JavaScript Example
<html>
<head>
<script type="text/javascript">
function MoveBack()
{
window.histoy.back();
}
function MoveForward()
{
window.histoy.forward();
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="Back" onclick="MoveBack()"/>
<input type="button" value="Forward" onclick="MoveForward()"/>
</form>
</body>
</html>