JavaScript - Javascript Cookie Exist Else Script
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 Similar TutorialsHi 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; } }; Never mind. I found a solution to get it working. I still don't know why it occurred, but I don't have the time to fulfill my curiosity like I could when I was a teenager. Hi, I want to add a cookie in this script to show the popunder once a day. Can someone help me to change the following code to make it work ? Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Popunder</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> </head> <body> <form id="popSubmit" action="http://www.google.com"> <input type="submit" /> </form> <script type="text/javascript"> (function($) { $.popunder = function(sUrl) { var _parent = self; var bPopunder = ($.browser.msie && parseInt($.browser.version, 10) < 9); if (top != self) { try { if (top.document.location.toString()) { _parent = top; } } catch(err) { } } var sOptions = 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=' + (screen.availWidth).toString(); sOptions += ',height=' + (screen.availHeight).toString() + ',screenX=0,screenY=0,left=0,top=0'; var popunder = _parent.window.open(sUrl, 'pu_' + Math.floor(89999999*Math.random()+10000000), sOptions); if (popunder) { popunder.blur(); if (bPopunder) { window.focus(); try { opener.window.focus(); } catch (err) { } } else { popunder.init = function(e) { with (e) { (function() { if (typeof window.mozPaintCount != 'undefined') { var x = window.open('about:blank'); x.close(); } try { opener.window.focus(); } catch (err) { } })(); } }; popunder.params = { url: sUrl }; popunder.init(popunder); } } return this; } })(jQuery); $('#popSubmit').submit(function() { jQuery.popunder('http://www.facebook.com'); }); </script> </body> </html> I have been trying for some time now to find a script to create a cookie for the following, so that the user selected font size is remebered throughtout the site. I have traweled the internet for some time now but all to no avail, the only guides i can find are for resizing a single element, which is no good for what i require. The code i already have is: <script type="text/javascript"> //Specify affected tags. Add or remove from list: var tgs = new Array( 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'b', 'li', 'form.button' ); //Specify spectrum of different font sizes: var szs = new Array( 'x-small','small','medium','large','x-large' ); var startSz = 2; function ts( trgt,inc ) { if (!document.getElementById) return var d = document,cEl = null,sz = startSz,i,j,cTags; sz += inc; if ( sz < 0 ) sz = 0; if ( sz > 5 ) sz = 5; startSz = sz; if (!( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ]; cEl.style.fontSize = szs[ sz ]; for ( i = 0 ; i < tgs.length ; i++ ) { cTags = cEl.getElementsByTagName( tgs[ i ] ); for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ]; } } </script> On a seperate issue i would also like to know if there is a script i can put in to enable users to quickly return to the default font size (although it would not be the end of the world if such a thing is not possible). 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!')} } Hi 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 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 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 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. 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, 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! 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. My javascript compiler is telling me that my function doesn't exist. However its just because the function isn't compiling right... if someone could give it a look and tell me what i'm doing wrong i would greatly appreciate it. Code: <script type="text/javascript"> function Country(f,s,h,k) { var Country=['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Missippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dekota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming']; var first=$('#'+f).val(); var second=$('#'+s); var hidden=$('#'+h); if(first=="USA") { hidden.css('display','block'); var options=""; for(var i=0;i<=(Country.length-1);i++) { options+="<option value='"+Country[i]+"'>"+Country[i]+"</option>"; } second.html("<option selected='selected'>Choose one...</option>"+options); } else { hidden=$('#'+k); hidden.css('display','block'); } } </script> Code: <tr style="background-color: #f4fbee"> <td width="324"><font face="verdana" size="2">9.) Country</font></td> <td width="324"> <select name="Country" id="Country" onchange="Country('Country','state','stateother1','stateother2');"> <option value="none" selected="selected">Choose one...</option> <option value="Australia">Australia</option> <option value="Canada">Canada</option> <option value="UK">United Kingdom</option> <option value="USA">United States</option> <option value="Other">Other</option> </select> </td> </tr> that is where I call the function. i have a problem. i want to check that whether an element with a specific id exist on page or not. example Code: <script type="text/javascript"> function bgcolo() { if(document.getElementById("jj") == null) { return red; } else { return green; } } </script> </head> <body> <table width="100%" border="1"> <tr> <td id="oo" bgcolor="return bgcolo();">bvbv</td> </tr> </table> <div id="jj">fggftrytyyyyyyyyy</div> </body>< Hi all, I am working with the following code: Code: window.onload = (window.onload) ? window.onload : function () { for (var i = 0; i < onload_functions.length; i++) { eval(onload_functions[i]); } } ...and it's used like this: onload_functions.push('runSomeJS()'); The idea is that IF the "window.onload" function doesn't exist, I supply my own. My problem is that this code also uses an array: var onload_functions = new Array(); Now this array will not be present if the "window.onload" function is not present. So I need to also define it conditionally. I tried this: onload_functions = (onload_functions) ? onload_functions : new Array(); ...but it doesn't work. I also tried: onload_functions = (onload_functions) ? onload_functions : onload_functions = new Array(); ...doesn't work either. I'm sure the solution is simple, but I'm just not seeing it. Any ideas? Thanks! -- Roger iframe Info: id="AssessmentsIFrame" button Info: id="wpv_1266351" Hi, I was wondering how to load an external .php script file in javascript, and make use of a php variable in the javascript. Thanks in advance. EDIT: If this is not possible, is there some way to send the php data to the javascript by using php, and then accessing the data that was sent in the javascript, and making use of it? What I'm trying to do is get a OpenTok session id from a .php script to javascript. |