javascript cookie

javascript cookie

View Answers

August 17, 2009 at 4:59 PM

Hi Friend,

Try the following code to set the cookie and expire the cookie:

<html>
<head>
<script type="text/javascript">
function getCookie(cookieName){
if (document.cookie.length>0) {
start=document.cookie.indexOf(cookieName + "=");
if (start!=-1) {
start=start + cookieName.length+1;
end=document.cookie.indexOf(";",start);
if (end==-1) end=document.cookie.length;
return unescape(document.cookie.substring(start,end));
}
}
return "";
}
function setCookie(cookieName,value,expiredays){
var expireDate=new Date();
expireDate.setDate(expireDate.getDate()+expiredays);
document.cookie=cookieName+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+expireDate.toGMTString());
}
function check(){
username=getCookie('username');
if (username!=null && username!="") {
alert('Welcome again '+username+'!');
}
else {
username=prompt('Enter your name:',"");
if (username!=null && username!="") {
setCookie('username',username,1);
}
}
}
</script>
</head>

<body onload="check()">
</body>
</html>

Thanks









Related Tutorials/Questions & Answers:
javascript cookie - Java Beginners
javascript cookie  hi all, i wanted to know how to set expire attribute of cookie in javascript. heres the code : document.cookie = 'cookie2=Second_cookie'; expires=Mon, 17-Aug-09 04:42:00 GMT; path="/"'; whats wrong
JavaScript
JavaScript  clone JavaScript
Advertisements
javascript
javascript  javascript code to dynamically add table on button click.../javascript/javascriptexamples/javascript-add-row-dynamically.shtml http://www.roseindia.net/javascript/javascript-add-row-to-table.shtml http://www.roseindia.net
Javascript
Javascript  How validations are done using javascript ,with example? and interview questions on javASCRIPT
JavaScript
should use JavaScript? Thanks   Hi, JavaScript is scripting language that runs on browser. You have to embed JavaScript in HTML page. Please see JavaScript tutorial. Thanks
JavaScript
JavaScript  how to get full path of a file type in javascript
javascript
javascript  Hi deepak, how to write form validation on javascript
javascript
javascript  write a program to display implement about browsers using javascript
javaScript
javaScript  How to open a browser window that cannot be resized? (HTML + Javascript
javascript
javascript  i have just learned javascript... can you tell me what kind of applications can be made by javascript?? thank you
javascript
javascript  how to set the request or session attribute in javascript head part of jsp page
javascript
javascript  Hi sir, This is sinduri, i want to learn javascript, so plz can u help me.how to learn
javascript
javascript  hi sir, if i want to learn javascript. what concepts i want to know
JAVASCRIPT
JAVASCRIPT  I have one textbox and I want to validate that it must start with number(1-0). can anyone tell me a javascript for that ? thanks in advance
Javascript
Javascript   Javascript to check Numeric entry in checkbox.....   Hi Please find the following code for numeric entry validation in javascript function validateBox(){ var data=document.myForm.someText.value
javascript
javascript  hi, I was actually working on to calculate the number of days between two dates of yyyy-mm-dd format using javascript, and when i click on button then number of days should be display in textbox
javaScript
javaScript  . Print a table like below in JAVASCRIPT 5 x 1 = 5 5 x 2 = 10 ΓΆβ?¬Β¦ΓΆβ?¬Β¦ΓΆβ?¬Β¦. 5 x 20 = 100
javascript
javascript  passing javascript values to jsp   Hi Friend, Try the following code:ADS_TO_REPLACE_1 form.jsp: <html> <script>... Javascript value In JSP"> <% String st=request.getParameter("msg"); if(st
Javascript
this format xxx-xxx-xxxx, i want the code in javascript. as a function.   Javascript Phone Number Validation <html> <script> function..." method="post" onsubmit="javascript:return validate();"> Phone Number:<
Javascript
Javascript  Dear Sir, Thank You a lot for your continuos support.Again i am in need of your help. What is the javascript code to disable a link,once... javascript or css Please help me.Urgent Regards Debasis   Hello Friend
Javascript
Javascript  <html> <head> <script type="text/javascript"> function validateForm() { var sname=document.getElementById('spocname'); var scontact=document.getElementById('spoccontact'); if(sname.value.length
javascript
javascript
javascript  how to create random question using javascript?   <html> <script> function displayQuestions() { document.getElementById("text").value=" "; var Questions = new Array(20
javascript
Clear cookie example
. The example below shows how to delete the cookie from the browser using JavaScript. The code below has saveCookie() method that is used to create the cookie... Clear Cookie Example    
javascript
javascript  Hi deepak, sample example on javascript form validation   <html> <script> function checkName(text) { if(isNaN(text)){ return 1; } else{ alert("Please enter a valid name. The only charachters
Cookie in jsp
Cookie in jsp  Define Cookie in jsp ?   The cookie file is a file that resides on the client machine. It contains data passed from web... returns. The web site only has access to the part of the cookie file that represents
Persistent Cookie
Persistent Cookie  What Is a Persistent Cookie?   Hi friends, A persistent cookie is a cookie which is stored in a cookie file permanently... because users can open cookie files see the cookie values. Thanks
set cookie in jsp
set cookie in jsp  How set cookie in jsp ?   Cookie... cookie to identify the user in the next time visit.Example : <% Cookie cookie = new Cookie("ClientName","Roseindia"); cookie.setMaxAge(3600
How to reset a cookie?
How to reset a cookie?  How to reset a cookie
How to destroy a cookie?
How to destroy a cookie?  How to destroy a cookie
What Is a Persistent Cookie?
What Is a Persistent Cookie?  What Is a Persistent Cookie?. Explain
JavaScript Cookies
; In this section, you will learn cookies in JavaScript. A cookie is a temporary file... JavaScript Cookies... example we have created a cookie that stores the name of a visitor for 1 day. 
Setting Cookie
Setting Cookie  setcookie function is used for setting up a cookie. In the following example, if you use $_COOKIE command, you will view one cookie value, and if you see print_r ($_COOKIE), you will view all cookie value. We have
Cookie methods in jsp
Cookie methods in jsp   Define cookie methods in jsp ?    Cookie methods : clone() getComment() getDomain() getMaxAge() getName() getPath() getSecure() getValue() getSecure() getVersion
Cookie Handling - JSP-Servlet
Cookie Handling  Hi i am working on the Application in which i... on a per user setting (probably via a cookie). This would of course mean that when..."; //get the array of cookies. Cookie cookies [] = request.getCookies
Write cookie and session to textfile?
Write cookie and session to textfile?  I want to store all data from user submisstion into a textfile, include session and cookie. Thanks
Removing existing cookie in jsp
Removing existing cookie in jsp  How remove existing cookie in jsp ?   If you want to remove an existing cookie, you can use the method setMaxAge() of that cookie object to set its timeout to zero.Example <%@page
JavaScript types
JavaScript types  What are JavaScript types
Delete Cookie
the cookie through setting session.  In the given example, we have set the value of cookie after one month of generating and before one hour of system date. ADS_TO_REPLACE_1 For deleteing the cookies, you will have to set the cookie
problem with cookie - Struts
problem with cookie  struts cookies response - Hii, I am a beginner in struts..Well, I try to add username and group(or whatever) to a cookie but not able to access the cookie value in the next page after submitting the form
Is session depend on cookie ???
Is session depend on cookie ???  Since I created one session & as we say that session store at server side that means if I clear browser cookie... the cookie then my user logged out that means there is something behind session
Javascript Examples
JavaScript Examples     Clear cookie example Cookies can...; JavaScript Cookies A cookie is a temporary file that contains the visitor's... to any past date. The example below shows how to delete the cookie from the browser
javascript doubt
javascript doubt  How to retrieving the values from Database by using javascript
JavaScript and ECMAScript
JavaScript and ECMAScript  What?s relationship between JavaScript and ECMAScript
javascript validation
javascript validation  validation of comparing dropdownlist and textbox in javascript
JavaScript - JavaScript Tutorial
JavaScript Tutorials is one of the best Quick reference to the JavaScript. In this JavaScript reference you will find most of the things about java script. JavaScript tutorial is classified into sections with examples. This tutorial
JavaScript - JavaScript Tutorial
JavaScript Tutorials is one of the best Quick reference to the JavaScript. In this JavaScript reference you will find most of the things about java script. JavaScript tutorial is classified into sections with examples. This tutorial
cookie and session dependency
cookie and session dependency  Hi, I am currently working on a project in travel domain. Throughout the application, we have not used cookie for session tracking but the case when browser cookie is disabled, session value differs
How can we destroy the cookie?
How can we destroy the cookie?   How can we destroy the cookie

Ads