0% found this document useful (0 votes)
61 views10 pages

##Write A Javascript Code of Create A Cookie##

The document provides JavaScript code examples for working with windows and cookies: 1) Code is provided to create, read, delete, and set expiration dates for cookies. Additional code opens new windows, sets window properties like size and position, and closes windows. 2) Methods like setTimeout, setInterval, and getting window properties like path, hostname, and protocol are demonstrated. Code provides ways to scroll windows and change window content. 3) Examples show opening multiple windows using loops and creating new webpages in windows with JavaScript. Getting the browser history is also demonstrated.

Uploaded by

Rohi shewalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views10 pages

##Write A Javascript Code of Create A Cookie##

The document provides JavaScript code examples for working with windows and cookies: 1) Code is provided to create, read, delete, and set expiration dates for cookies. Additional code opens new windows, sets window properties like size and position, and closes windows. 2) Methods like setTimeout, setInterval, and getting window properties like path, hostname, and protocol are demonstrated. Code provides ways to scroll windows and change window content. 3) Examples show opening multiple windows using loops and creating new webpages in windows with JavaScript. Getting the browser history is also demonstrated.

Uploaded by

Rohi shewalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

4##Write a javascript code of create a cookie##

<!DOCTYPE html>
<html>
<head> <script>
function createCookie(){ var
cookie_value = username.value;
document.cookie = "username=" + cookie_value;
alert("Cookie is created");
}
</script>
</head>
<body>
Enter Username: <input type='text' id='username'>
<input type= 'button'value='create cookie' onclick='createCookie()'>
</body>
</html>

##Write a javascript code to read the cookie value##


<html>
<head>
<script type="text/javascript">
function readCookie()
{
var cookies = document.cookie; document.write("All
stored Cookies :" + cookies);
var cookiearray = cookies.split(';'); for(var i=0;
i<cookiearray.length; i++) { var cookie_pair =
cookiearray[i]; var name_value_array =
cookie_pair.split('='); var name =
name_value_array[0]; var name =
name_value_array[1];
document.write("<br>Name is:"+ name +"and Value is:"+value);
}
}
</script>
</head>
<body>
<P>Click the following button to get the Cookies:</p>
<input type = "button" value = "Get Cookie" onclick="readCookie()"/>
</body>
</html>
##write a javascript code to delete the cookie##
<!DOCTYPE html>
<html>
<head> <script> function deleteCookie(){ var cookie_value = username.value;
document.cookie = "username="+cookie_value+";expires=Thu,01 Nov 2022 00:00:01 GMT";
alert("Cookie is Deleted");
}
</script>
</head>
<body>
Enter Username: <input type= 'text id='username'>
<input type= 'button' value='Delete cookie' onclick='deleteCookie()'>
</body>
</html>

##write a javascript code to set the expiration date a cookie##


<html>
<head>
<script type="text/javascrpit"> function changedate() { var now = new Date();
now.setMonth(now.getMonth() + 1); var cookie_value = username.value;
document.cookie = "username=" + cookie_value+";expires=" + now.toUTCString();
alert("Expiry Date of Cookie changed:" + "username=" + cookie_value);
}
</script>
</head>
<body>
Enter cookie name: <input type = "text" id = "username"/>
<input type = "button" value = "Extend Expiry Date" onclick = "changedate()"/>
</body>
</html>

##Write a javascript to open a new window##


<html>
<head>
<script language=”javascript” >
function openWindow() {
window.open(“”,””,”width=200,height=100”);
}
</script>
</head>
<body>
<p>Click to open new window</p>
<input type= “button” value=”open new window” onclick=”openWindow()”/>
</body>
</html>

##write a javascript code to set the window focus##


<html>
<head>
<script language=”javascript” >
function openWindow() {
window.open(“”,””,”width=200,height=100”);
}
</script>
</head>
<body>
<p>Click to open new window</p>
<input type= “button” value=”open new window” onclick=”openWindow()”/>
</body>
</html>

##write a javascript to set the window position##


<html>
<head>
<script language=”javascript” >
function openWindow() {
window.open(“”,””,”left=0,top=0,width=200,height=100”);
}
</script>
</head>
<body>
<p>Click to open new window</p>
<input type= “button” value=”open new window” onclick=”openWindow()”/>
</body>
</html>

##Write a javascript to change the content of whole window##


<html>
<head>
<script type = "text/javascript">
function changeContent() {
document.write("<h3><p> After clicking the Button: New Content</p></h3>");
}
</script>
</head>
<body>
<p>Click to change the content of whole Window</p>
<input type = "button" value = "Change Content" onclick =
"changeContent()"/>
<h3> <p id="para"> Before Clicking the Button: Old Content</p> </h3>
</body>
</html>

##Write a javascript code to close a window##


<html>
<head>
<script type = "text/javascript">
function openWindow() {
new_win = window.open("","","width=200,height=100");
}
function closeWindow() {
new_win.close();
}
</script>
</head>
<body>
<p>Click to open new window</p>
<input type = "button" value = "open new window" onclick = "openWindow()"/> <p>Click
to Close new window</p>
<input type = "button" value = "close new window" onclick = "closeWindow()"/> </body>
</html>
##Write a javascript code to scroll the window to the particular position##
<html>
<head>
<script type = "text/javascript">
function scrollWindow() {
scrollTo(0,200);
}
</script>
</head>
<body>
<p>Click to scroll window</p>
<input type = "button" value = "scroll window" onclick = "scrollWindow()"/>
<p> A </p>
<p> B </p> <p>
C </p>
<p> D </p>
<p> E </p>
<p>---------------Window is Scrolled.</p>
</body>
</html>

## Write a javascript code to scroll the window by specified pixels##


<html>
<head>
<script type = "text/javascript">
function scrollWindow() {
scrollTo(0,100);
}
</script>
</head>
<body>
<p>Click to scroll window</p>
<input type = "button" value = "scroll window" onclick = "scrollWindow()"/>
<p> A </p>
<p> B </p> <p>
C </p>
<p> D </p>
<p> E </p>
<p>---------------Window is Scrolled.</p>
</body>
</html>
##

Write a javascript code to open multiple window ##


<!DOCTYPE html>
<html>
<head> <script>
function openWindow()
{ window.open("","","width=200,height=100");
window.open("","","width=200,height=100");
}

</script>
</head>
<body>
<p>Click the button to open multiple window.</p>
<input type="button" onclick="openWindow()" value="Open Windows">
</body>
</html>

##Write a javascript code to open multiple window by using for loop##


<!DOCTYPE html>
<html>
<head> <script>
function openWindow() {
for(i=0;i<5;i++){
window.open("","","width=200,height=100");
}
}
</script>
</head>
<body>
<p>Click the button to open multiple window.</p>
<input type="button" onclick="openWindow()" value="Open Windows">
</body>
</html>
##write a javascript code to create a webpage in new window##
<html>
<head>
<script type = "text/javascript">
function openWindow() {
var new_win = window.open("New Window","","width=300,height=300");
new_win.document.write("<html>"); new_win.document.write("<head>");
new_win.document.write("<title>"); new_win.document.write("New Window");
new_win.document.write("</title>"); new_win.document.write("</head>");
new_win.document.write("<body>"); new_win.document.write("<h3> <p> Welcome
to the New Window.</p> </h3>"); new_win.document.write("<h4> <p> This
Webpage is created in New Windiw.</p>
</h4>");
new_win.document.write("</body>");
new_win.document.write("</html>");
}
</script>
</head>
<body>
<p> Click to open a new window </p>
<input type = "button" value = "open new Window" onclick = "openWindows()"/>
</body>
</html>

##write a javascript to code for setTimeout()##


<html>
<head>
<script type="text/javascript">
function myFunction() {
setTimeout(displayFunction, 5000);
}
function myFunction() {
alert("Time out");
}
</script>
</head>
<body>
<p>Click to set Time Out</p>
<input type = "button" value = "Set Time Out" onclick = "myFunction();"/>
</body>
##

</html>
Write a javascript code for setInterval()##
<html>
<head>
<script type = "text/javascript">
function myFunction()
{ setInterval(displayFunction,3000);
}

function displayFunction() {
document.getElementById("para").innerHTML+="<p> Welcome to Javascript.</>"
}
</script>
</head>
<body>
<p>Click to The set Time Interval</p>
<input type = "button" value = "Set Time Interval" onclick = "myFunction();"/>
<p id = "para"> </p>
</body>
</html>

##Write a javascript code to get a Pathname##


<html>
<head>
<script type = "text/javascript"> function displayFunction()
{ document.getElementById("para").innerHTML="The webpage is located
at:
"+window.location.pathname;
}
</script>
</head>
<body>
<p>Click to get The Path of the Web Page</p>
<input type = "button" value = "Get the Path" onclick = "displayFunction();"/>
<p id = "para"> </p>
</body>
</html>
##Write a javascript code to get the hostname##
<html>
<head>
<script type = "text/javascript"> function displayFunction()
{ document.getElementById("para").innerHTML="The host of the webpage
is :
"+window.location.Hostname;
}
</script>
</head>
<body>
<p>Click to get The Host of the Web Page</p>
<input type = "button" value = "Get the Host" onclick = "displayFunction();"/>
<p id = "para"> </p>
</body>
</html>

##Write a javascript code to get the protocal##


<html>
<head>
<script type = "text/javascript">
function displayFunction() {
document.getElementById("para").innerHTML="The protocol of the webpage is:"
+window.location.protocol;
}
</script>
</head>
<body>
<p>Click to get The protocol of the Web Page</p>
<input type = "button" value = "Get the protocol" onclick = "displayFunction();"/>
<p id = "para"> </p>
</body>
</html>
##

write a javascript to get a history list##


<html>
<head>
<script type = "text/javascript">
function previous() {
window.history.back();
}
function next() {
window.history.forword();
}
</script>
</head>
<body>
<input type = "button" value = "Previous" onclick = "previous();"/>
<input type = "button" value = "next" onclick = "next();"/>
</body>
</html>

You might also like