top of page

Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

1

What are Cookies?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Cookies are text files used to identify your computer as you use a computer network with small pieces of data like a username and password.HTTP cookies are used to identify particular users and improve web browsing experience.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

How can Cookies be created in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Step 1: By using document.cookie a cookie can be created:
document.cookie="name=value";
You have to give it three bits of information when calling createCookie() which is the name and value of the cookie and the number of days it is to remain active.
Step 2: The name-value pair should become ppkcookie=test cookie in this case and should be active for 7 days.
createCookie('ppkcookie','testcookie',7)
The cookie is trashed if you set the number of days to 0 when the user closes the browser.The cookie is trashed immediately if you set the days to a negative number .
Step 3: The function starts doing its job when arguments are received.
function createCookie(name,value,days) {
i) If day value is present then only we need to do the time calculation.
if (days) {
ii) If create a new Date object containing the current date is present .
var date = new Date();
Add the required number of days (in milliseconds) to the current time in milliseconds . Once the value is set the time of the date to this new value, date will be in milliseconds so now cookie should expire in that time.
date.setTime(date.getTime()+(days*24*60*60*1000));
Step 4: The variable expires should set to this date in the UTC/GMT format required by cookies.
var expires = "; expires="+date.toGMTString();
}
Expires is not set and the cookie expires when the user closes his browser,if 0 is passed to the function.
else var expires = "";
Step 5: At last write the new cookie into document.cookie in the correct syntax.
document.cookie = name+"="+value+expires+"; path=/";
}

LRM_EXPORT_207556595493866_20190724_1939

Q

3

How can Cookies be read using JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Step 1: Call the following function and pass the name of the cookie to read the cookies. Put the name in a variable. Check if this variable has a value (if the cookie does not exist the variable becomes null, which might upset the rest of your function), then do whatever is necessary.
var x = readCookie('ppkcookie1')
if (x) {
[do something with x]
}
Step 2: The function receives the argument and starts.
function readCookie(name) {
Step 3: We're going to search for the name of the cookie, followed by an =. Create new string and assign it in nameEQ:
var nameEQ = name + "=";
Step 4: Then split document.cookie on semicolons. Now ca is an array having all cookies which are set for this domain and path.
var ca = document.cookie.split(';');
Step 5: Then we go through all cookies i. e. nothing but array.
for(var i=0;i < ca.length;i++) {
c should be set to the cookie to be checked.
var c = ca[i];
Step 6: If the first character is a space, remove it by using the substring() method. When the first character is not a space stop doing this.
while (c.charAt(0)==' ') c = c.substring(1,c.length);
Step 7: Now string c starts with the name of the current cookie. If this is the name of the cookie you are looking for

if (c.indexOf(nameEQ) == 0)
we've found the desired cookie .

Step 8: Now return the value of the cookie, which is the part of c that comes after nameEQ. We also end the function by returning this value.
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}

Step 9) We return null after we haven't found the name we're looking for in entire array, then cookie is not present
return null;
}

LRM_EXPORT_207556595493866_20190724_1939

Q

4

How can Cookies be deleted using JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Cookies can be deleted using erase. Erasing is extremely simple.
eraseCookie('ppkcookie')
Name of the cookie should be passed which you want to be erased
function eraseCookie(name) {
and to set the cookie with an expiry date of one day ago call createCookie() .
createCookie(name,"",-1);
}
The browser, detects that the expiry date has passed, then it removes the cookie.

LRM_EXPORT_207556595493866_20190724_1939

Q

5

What is the time a cookie can last?

LRM_EXPORT_207556595493866_20190724_1939

Ans

An arbitrary expiry time of cookie can be set at the time cookie is generated. By default it is active till the current browser window is open.

LRM_EXPORT_207556595493866_20190724_1939
bottom of page