Unit 4 - 45256988 - 2024 - 10 - 24 - 08 - 54
Unit 4 - 45256988 - 2024 - 10 - 24 - 08 - 54
A cookie is a small text file that a web site saves on your PC, mobile phone or other device,
containing information about your browsing on that site.
Cookies are necessary to facilitate browsing and make it more convenient; they do not harm
your computer.
Basics of Cookies
Cookies is a small piece of data stored in a file on your computer.
The information present in the cookies is accessed by the web browser.
A cookie contains the information as a string generally in the form of a name-value pair
separated by semi-colons.
It maintains the state of a user and remembers the user's information among all the
web pages.
Cookie is a small text file which contains following data:
- A name-value pair containing the actual data
- An expiry date after which it is no longer valid
- The domain and path of the server
Cookies remembers the information about the user in the following ways -
Step 1: When the user visits the web page his/her name can be stored in a cookie
Step 2: Next time when the user visits the page, the cookie remembers his/her name.
There are two types of cookies: Session cookies and persistent cookies
Session Cookies : Session cookies is a cookie that remains in temporary memory only
while user is reading and navigating the web site. The cookie is automatically deleted
when the user exits the browser application.
Q] Describe, how to read cookie value and write a cookie value. Explain with
example. [W-19][6 M]
Creating Cookies
JavaScript can create, read or delete a cookie using document.cookie property
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; key2 = value2; expires = date";
document.cookie = "username=Arrow";
<html>
<head>
Now your machine has a cookie called name. You can set multiple cookies using multiple key =
value pairs separated by comma.
It is a common practice to create a cookie and then read the value of the created cookie.
The document.cookie string will keep a list of NAME=VALUE pairs separated by semicolons,
name is the name of a cookie and value is its string value.
Using the split() function the string of cookies is break into key and values.
The split() method finds the = character in the cookie, and then take all the characters to the left of
the = and store them into array [0].
Next the split() method takes all the characters from the right of the = up to semicolon ( ; ) but not
Including the semicolon, and assign those characters to array[1]
Thus we can obtain the name value pair in array[0] and array[1] respectively.
name= Subject
value = CSS
<html>
<head>
<script type = "text/javascript">
function ReadCookie()
{
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies +”<br>”);
<body>
<form name = "myform" action = "">
<p> click the following button and see the result:</p>
<input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/>
</form>
</body>
</html>
Note − There may be some other cookies already set on your machine. The above code will display
all the cookies set on your machine.
function WriteCookie() {
</script>
</head>
<body>
<form name = "myform" action = "">
Deleting a Cookie
Sometimes you will want to delete a cookie so that subsequent attempts to read the cookie return
nothing. To do this, you just need to set the expiry date to a time in the past.
Example
Try the following example. It illustrates how to delete a cookie by setting its expiry date to one
month behind the current date.
<html>
<head>
<script type = "text/javascript">
function WriteCookie() {
var now = new Date();
now.setMonth( now.getMonth() - 1 );
cookievalue = escape(document.myform.customer.value) + ";"
</script>
</head>
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/>
</form>
</body>
</html>
name Optional. Specifies the target attribute or the name of the window. The following
values are supported:
_blank - URL is loaded into a new window, or tab. This is default
_self - URL replaces the current page
_parent - URL is loaded into the parent frame
_top- URL replaces any framesets that may be loaded. ( A collection of frames in
the browser window is known as a frameset.)
style Optional. A comma-separated list of items, no whitespaces. The following values are
supported:
fullscreen=yes|no|1|0 Whether or not to display the browser in full-screen
mode. Default is no.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an about:blank page in a new browser window that is 200px
wide and 100px tall.</p>
<script>
function myFunction()
{
var myWindow = window.open("", "", "width=200,height=100");
}
</script>
</body>
</html>
Window Position
We can set the desired position for the window.
Using the left and top attribute values the window position can be set.
Example : left and top attribute
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an about:blank page in a new browser window that is 200px
wide and 100px tall.</p>
<script>
function myFunction()
{
var myWindow = window.open("", "", "width=200,height=100,top=100,left=300");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to put the new window in the current window.</p>
<script>
function myFunction()
{
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new browser window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open("https://arrowcomputeracademy.business.site/");
}
</script>
</body>
</html>
<html>
<body>
<p>Click the button to open a new tab </p>
<button onclick="NewTab()">Open Window</button>
<script>
function NewTab() {
var mywin=window.open("","","width=200,height=200")
mywin.document.write("<p>This line is written in current window</p>")
The most simple operation about the window is to close it. It can be closed using the function
close()
<!DOCTYPE html>
<html>
<body>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
Example
<html>
<body>
<p>Click the button to open a new tab </p>
<button onclick="NewTab()">Open Window</button>
<script>
function NewTab()
{
for(i=0;i<5;i++)
<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');
}
</script>
</body>
</html>
window.clearTimeout(timeoutVariable)
Example:
<!DOCTYPE html>
<html>
<body>
<p>Click "Try it". Wait 3 seconds. The page will alert "Hello".</p>
<p>Click "Stop" to prevent the first function to execute.</p>
<p>(You must click "Stop" before the 3 seconds are up.)</p>
<script>
function myFunction()
{
alert("Hello");
}
</script>
</body>
</html>
scrollTo()
The scrollTo() method scrolls the document to specified coordinates.
For the scrollTo() method to work, the document must be larger than the screen, and the scrollbar
must be visible.
Syntax
window.scrollTo(x, y)
or
scrollTo(x, y)
Parameters
x Required. The coordinate to scroll to (horizontally), in pixels.
y Required. The coordinate to scroll to (vertically), in pixels.
Return Value NONE
<html>
<head>
<script type = "text/javascript">
function scrollwindow()
{
window.scrollTo(0,200);
}
<br><br><br><br><br><br>
<p>B</p>
<br><br><br><br><br><br>
<p>C</p>
<br><br><br><br><br><br>
<p>D</p>
<br><br><br><br><br><br>
<p>E</p>
<br><br><br><br><br><br><br><br>
<P>-------------Window Scrolled</p>
</body>
</html>
1. window.location.pathname :
Gives complete path at which the web page is located.
2. window.location.hostname:
Gives name of host on which the webpage is running. It gives domain name
3. window.location.protocol:
Gives the protocol used for the webpage. Such as HTTP, HTTPS
<html>
<head>
<script type="text/javascript">
function display()
{
<html>
<head>
<script type="text/javascript">
function display()
{
document.getElementById("ID").innerHTML = "The hostname of Web Page is:" +
window.location.hostname;
}
</script>
</head>
<body>
<input type="button" value="GetHost" onclick="display()">
<p id="ID"></p>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function previous()
{
window.history.back();
}
function next()
{
window.history.forward();
}
</script>
</head>
<body>
</body>
</html>
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
Example
alert("I am an alert box!");
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Alert</h2>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert("I am an alert box!");
}
</script>
</body>
</html>
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
window.confirm("sometext");
if (confirm("Press a button!"))
{
txt = "You pressed OK!";
} else
{
txt = "You pressed Cancel!";
}
</body>
</html>
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after
entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns
null.
Syntax
window.prompt("sometext","defaultText");
The window.prompt() method can be written without the window prefix.
Example
var person = prompt("Please enter your name", "Harry Potter");
var text;
if (person == null || person == "") {
text = "User cancelled the prompt.";
} else {
text = "Hello " + person + "! How are you today?";
}
Program
<!DOCTYPE html>
<html>
<h2>JavaScript Prompt</h2>
<p id="demo"></p>
<script>
function myFunction() {
var text;
var person = prompt("Please enter your name:", "Harry Potter");
if (person == null || person == "") {
text = "User cancelled the prompt.";
} else {
text = "Hello " + person + "! How are you today?";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
Line Breaks
To display line breaks inside a popup box, use a back-slash followed by the character n.
Example
alert("Hello\nHow are you?");
Program
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>Line-breaks in a popup box.</p>
</body>
</html>