JavaScript - Javascript Get/del/set Cookie Help !
anyone can help me in cookie ? i couldn't get it to work. My objective is when i click the submit button in the guestbook page, the cookie will then capture just the 'name' and display it out in thankyou page.
guestbook page var seconds = 0; function startTimer() { window.setInterval("updateTime()", 1000); } function updateTime() { seconds++; soFar.innerText = seconds; } function validate_email(field) { with (field) { apos=value.indexOf("@"); //find the position of the @ character dotpos=value.lastIndexOf(".");// find the last position of the . character if (apos<1||dotpos-apos<2) { alert("Not a valid e-mail address!"); return false; } } } function validate_length(field) { with(field) { if (value.length == 0) { alert("Please fill up the column!"); return false; } } } function getCookie(NameOfCookie){ if (document.cookie.length > 0) { begin = document.cookie.indexOf(NameOfCookie+"="); if (begin != -1){ begin += NameOfCookie.length+1; end = document.cookie.indexOf(";",begin); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(begin, end)); } } return null; } function setCookie(NameOfCookie, value, expiredays) { var ExpireDate = new Date (); ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 *3600 * 1000)); document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null)?"" : ";expires=" + ExpireDate.toGMTString()); } function delCookie (NameOfCookie){ if (getCookie(NameOfCookie)){ document.cookie = NameOfCookie + "=" + "; expires=Thu,01-JAN-70 00:00:01 GMT"; } } function validate_form(thisform) { with (thisform) { if (validate_length(yourname) == false) { yourname.focus(); return false; } if (validate_length(email)==false || validate_email(email)==false) { email.focus(); return false; } if ( ( document.contact_my.gender[0].checked == false ) && ( document.contact_my.gender[1].checked == false ) ) { alert ( "Please choose your Gender: Male or Female" ); valid = false; contact_my.focus(); return false; } } } </script> <form name="contact_my" action="thankyou.html" onSubmit="return validate_form(this)" && onClick="setCookie" method="post" > <input type="submit" value="Submit"> <input type="reset" value="Reset form" onClick="delCookie( 'username' )"> thankyou page function getCookie(NameOfCookie){ if (document.cookie.length > 0) { begin = document.cookie.indexOf(NameOfCookie+"="); if (begin != -1){ begin += NameOfCookie.length+1; end = document.cookie.indexOf(";",begin); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(begin, end)); } } return null; } function setCookie(NameOfCookie, value, expiredays) { var ExpireDate = new Date (); ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 *3600 * 1000)); document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null)?"" : ";expires=" + ExpireDate.toGMTString()); } function DoTheCookieStuff() { username=getCookie ('username'); if ( username!=null ) {document.writeln ('Hi there '+username+' - Good to see you again!')} } Similar TutorialsHi i used cookies to store data using java script with 2 calculators, but there is a 3rd calculator that the cookies arent storing, can someone help me figure out where to implement the script?
Hey guys, i'm trying to incorporate cookies with the website i'm working on. I've looked at tutorials where they just hand you the code and say "get on with it", this leaves me in the lurch while trying to debug it... so the question is are there any good javascript cookie tutorials that will "explain what the heck this does?" Thanks Al. Hey guys, Here is my question: Is it possible to extract a cookie inside a textarea? For example, I am building a very basic shopping cart in Javascript, with "Add to Cart" buttons listed for each item. As the "Add to Cart" button is pressed, a cookie is created. There is a link in which you can click with "Review Order", which consists of a textarea that displays the items that you have 'purchased'. I am trying to get the value of the cookie to be extracted into this textarea, however I am not sure if this is possible. Thanks I am creating a cookie that increases a "value" or "integer" by 1 (ie Y+1) every time any page inside my webpage. Any webpage that i put the code into. right now I need it so that when you visit a page with the code it increases the variable or "cookie" (Y) by 1. when Y reaches a certain number, I.E 10, it resets Y to "0" and redirects you to a URL. Please explain where each value would go in any replies. Please make code as simple and as low in file size as possible. I don't need any sort of flexibility, just a simple redirect to a webpage which WILL NOT have the cookie code in it. EX: when opening the menu of an iPod game it asks you to rate the game, saying remind me later resets the value to "0" and saying rate now, resets to "0" but does not keep counting on the next page. Thanks to all in advance and i hope you completely understood my problem and question! [spoiler] Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script type="text/javascript"> function checkValue() String Y Y=Y+1 if(Y > 9){ alert(Y)} else{ alert(Y)} </script> </head> <body> <form action="Javascript:checkValue()" > <div align="center"> <input name="Logon" type="submit" value="Click Me"/> </div> <script type="text/javascript"> function checkValue() String Y; Y=Y+1; alert(Y); //if(Y > 9){ //alert(Y)} //else{ //alert(Y)} </script> </form> </body> </html> [/spoiler] I did not expect any of the above code to work, completely providing a new code will not surprise me or be a problem. Thanks again in advance! Hi all Experts I am in a need of a simple javascript script that can verify if a cokie exist If it exist: (replace the below and show) You already upgraded. If not exist: I would like it to show: You account is not upgraded yet Please upgrade your account, click here to upgrade<br> <center><A href="url"><img src="picture"></center> also need seperate script to set the cokie Hope u understand what I want thank you hello everybody, here I'm asking for help again. sorry for that in advance. anyhow, I have the following code: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link type="text/css" media="screen" rel="stylesheet" href="colorbox.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="../colorbox/jquery.colorbox.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".example7").colorbox({width:"300px", height:"500px", iframe:true, open: true}); }); </script> </head> <body onload="example7();"> <a class='example7' href="tofes.html"></a> teststestestest </body> </html> I want the lightbox window open only once a day for every unique visitor on my website. I understand I should use some sort of cookie implented, but I dont really understand how to. Thanks in advance, daniel. I need some help for University cousework that has been sent and I hope someone here can help. I need to create a cookie (easy enough) and then populate that cookie with a word that is selected when you click a certain image. Imagine you have image1.jpg and image2.jpg when a user selects image1.jpg the cookie needs to store image1 and load page2.html If the user selects image2, then page2.html still needs to load but store the word image2 in the cookie. On the next page, the cookie is loaded and the page needs to load the css file associated with that word example: image1 = firstcss.css image2 = secondcss.css Is this possible? Thank you for all your help Hi, I have a website where the user can navigate different categories by tab. The problem is, when they're on a tab other than the default one and they refresh the page or click to another page, it puts them back on the first tab. I know this can be solved by using a session cookie of some type, I just need some help on implementing it into my site. http://www.thatswhyimbroke.com/ - so you can see what i'm talking about. If you look at the different tabs: Price High, price low, food & drink, etc. I want the user to be able to go to one of those, click to page 2 or refresh, and still be on that tab. Any help would be much appreciated! I have been looking all over the web for a solution, because my website displays funky in safari. To fix this i figured i'd tell users that if they are using safari, then using javascript, i could tell them a message. Originally i tried detecting safari then displaying a message. But i couldn't ever find a browser detection for safari. So i thought wait.... If i could: 1. get a javascript alert saying my message. 2. time that message alert frequency by a session, say 10 days? or even every browser session... Then i could solve my problem. However i can't figure out any of this. So if anybody would be willing to help, i'd be very grateful. Hello, Could anyone help me? I'm looking for a script that remembers, re-sets, and can set to the previous state the show or hide behavior of a layer upon return to the page. I have an image gallery that's in 3 categories: wedding, bridal shower and save-the-dates. When viewing an image in the bridal shower or save-the date galleries and I press on the link I created titled "Back to Gallery" it does not return me back to the Bridal Shower or Save-the-dates gallery (whichever I'm in) it returns me to the main Invitations gallery page. So bascially I'm looking for a script that will write to a cookie what layers are shown and what are hidden so when you return it reads the cookie and displays the last layers you had showing before leaving the page. Hi everyone, I am using a jQuery cookie script to set the cookie of some elements on my website. One of the problems is that I need the cookie to not expire after one day, I need it to expire after a while (I'm going to start off with a year). Here's my script, the red part is what I've been editing. Code: /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ /** * Create a cookie with the given name and value and other optional parameters. * * @example $.cookie('the_cookie', 'the_value'); * @desc Set the value of a cookie. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secu true }); * @desc Create a cookie with all available options. * @example $.cookie('the_cookie', 'the_value'); * @desc Create a session cookie. * @example $.cookie('the_cookie', null); * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain * used when the cookie was set. * * @param String name The name of the cookie. * @param String value The value of the cookie. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. * If set to null or omitted, the cookie will be a session cookie and will not be retained * when the the browser exits. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will * require a secure protocol (like HTTPS). * @type undefined * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ /** * Get the value of a cookie with the given name. * * @example $.cookie('the_cookie'); * @desc Get the value of a cookie. * * @param String name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000 * 365)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; Hi im kind of new to cookies in Javascript. But after reading a few tutorials on how the work I started to wonder. Is it possible to grab a cookie made by my phpbb forum? I would love to be able to login on my site using my phpbb forum cookies. Anway if this is a bad idea ore won't work for any reason plz let me know. Thanks Hello. I am a neewb, so bare with me. This code is not working correctly for some reason. If I use it in Internet Explorer it will work, but only if you bring up the history in the url tab. You cannot refresh it for whatever reason. so basically it works in Explorer but no refresh. The big problem is Mozilla. I will not work at all. I have all of the cookies set for third party, remember last visit and so on. It will only display the welcome page for first time visitor. Then it will show the subsequent page, however it will not increment the count +1. I am not sure what is going on here, Explorer works, but with no refresh, and Mozilla does not really work at all? Here is my script currently: <script type="text/javascript"> /* <![CDATA[ */ function hitMySite() { var lastDate = new Date(); lastDate.setMonth(lastDate.getMonth()); var dateString = (lastDate.getMonth() + 1) + "/" + lastDate.getDate() + "/" + lastDate.getFullYear(); if (document.cookie != "") { counter = document.cookie.split("=")[1]; counter = parseInt(counter) + 1; date = document.cookie.split(":")[2]; var expireDate = new Date(); expireDate.setMonth(expireDate.getMonth() + 12); document.cookie = "counter=" + counter + ":date:" + dateString + ";expires=" + expireDate.toGMTString(); document.write("<h1>You last visited this page on: " + date + "<br />You have been to this page " + counter + " times.</h1>"); } else { document.write("<h1>Welcome to my Web site! This is your first visit here so be sure to bookmark my page!</h1>"); var counter = 1; var expireDate = new Date(); expireDate.setMonth(expireDate.getMonth() + 12); document.cookie = "counter=" + counter + ":date:" + dateString + ";expires=" + expireDate.toGMTString(); } } /* ]]> */ </script> </head> <body onload="hitMySite()"> </body> </html> Hi everyone! Got a quick (cookie) question. I looked on the internet for some cookie scripts (redirect ones), but unfortunately haven't been too lucky with these. What I want to do is the following: When the cookie is not found it goes to my main page, for example: "www.anynamehere.com" On the main page I can select where I want to go - 'Contacts', 'News', 'Forums' and so on. The main page has a drop down list with these options, each option has it's own link. When I click on 'continue' and go to the specific link (ex. www.anynamehere.com/contacts.html) it should remember my selection, so next time I log into www.anynamehere.com it's automatically will take me to contacts.html. I will not see the main page anymore. Now, if from the contacts page I select 'News' and go to www.anynamehere.com/news.html it has to remember that link as well. So if I close my browser and then reopen it, it should take me to www.anyname.com/news.html. I hope that makes sense. If anyone can give me any pointers I would greatly appreciate it. Maybe there is a script like that available online, I just wasn't lucky enough to find one. Thank you in advance! Hi im new to working with cookies so would appreaciate a little help I using the w3c tuturial as a template so heres a link so you can see what im trying to do http://www.w3schools.com/JS/js_cookies.asp Code: function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } } function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } function checkCookie(username) { var username=getCookie("username"); if (username!=null && username!="") { document.getElementById("feedback").innerHTML = " last time you scored " + username); setCookie("username",username,365); } else { if (username=null || username="") { setCookie("username",username,365); } } } I kept the variable names the same so you can follow and I don't confuse myself and make things more complected while im trying to debug it. Some extra information. checkcookie is been given a variable it is just a simple number. Im making a questionnaire which is done but I wont the top of the page to tell the user how much they scored last or tell them this is their first time trying it. The variable being passed to check cookie is their score . at the moment when I click the submit button nothing happens. Where it should trigger a score calculating function which should then call the checkcookie function while passing the score it calculated. I would love if someone can point me in the right direction or help me correct it or atleast explain to me whats going wrong. Thanks and a lots of appreciation if anyone can spare the time I cannot get the cookie to save, I'm pretty new at web designing in general. If you could, the name of the cookie be cirulcook, also any advice you would have for me would be great! Code: <script><!-- function SETcookie(){ document.cookie="Selected="+document.getElementById('myList').selectedIndex; } function GETcookie(){ if (document.cookie){ eval(document.cookie); document.getElementById('myList').selectedIndex=Selected; }}// --></script> </head> <body onLoad="GETcookie()"> <select id="myList" onChange="SETcookie()"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> </select> Hello all! I wanted to say hey as this is my first post! I will probably stick around for a little, but I need help first. I'm trying to make a code for something on a forum. It will prompt the user for a yes or cancel, then save a cookie with their answer. Then the next time the script is called, it will check for the cookie. If there is one saved, it won't prompt and use that value. Here's my breakdown (hope this is right) 1. Have a popup box that prompt for a 'yes' or 'cancel' Code: if(window.confirm("Use WHATEVER theme?")) { * * location.href = "URL HERE"; } Got that! 2. Save a cookie with the answer Here is where I need the help. I really don't have a clue other than the fact that you use setCookie(name, value, expire). I'm guessing that there will need to be something special in the value section. 3. When the script is loaded, it will check for the cookie saved and do whatever was saved Again, I need help. I know I'll need the getCookie function, but I dont know what else. As you can see, I'm rather hazy in the cookies department. If you ha any trouble and now understand it, please direct me to where you learned it as I am willing and wanting to learn this inportant function of JavaScript! I tried to provide as much detail as possible, but if you have questions ask! Any help would be greatly appreciated, whether or not it's the code I need. Whether it is direction or step by step help, I will still value it! Thanks! Hi, I want to know how we can get the cookie size. In Mozilla firefox i got Name Value Host Path Expire. The same way i want to get cookie size also . Please advise me how can i get it using javascript. Frnds,i need some help related to cookie handling in JS.Suppose i created a cookie with an expiration time of 1 month.Now,for the time my current browser session is running,the cookie is stored in browser memory.When the browser is closed,the cookie gets written to HD.(I need to know where it is written). Next day,when the script page is opened,the browser should check the cookie created last day and display its value in the page.. Does this operation handled by document.cookie call??I mean..setting cookie will definitely require this call,but what about fetching it back next day???what call should be used for fetching... |