JavaScript - Cookie Expiration In Seconds?
Trying to setup a cookie read/write in JS.
Page 1 sets the JS cookie using Flash. Page 2 reads the JS cookie from Flash. I have this working. The problem is that I need the cookie to expire in a matter of seconds. This is my code for creating the cookie and expiration: Code: function setCookie(name, value) { var today=new Date; today.setTime(today.getTime()+3000); document.cookie = name+"="+value; +"; expires="+today.toGMTString(); //alert(name + " "+value); alert("Value is: "+value+"\n Expires in: "+today); } The problem isn't that cookie isn't there. I just can't seem to get it to expire. Please help. Similar TutorialsHello all, I need some quick help with one of my scripts. It's supposed to set the expiration date of all the cookies on my page for one year, but it's not working. I know it's not working because Safari lets you see the expiration date for each cookie set on a page, and it didn't show it for the elements being set on my page. I'm using jQuery for this, so heres my one script: Code: var currentRotation=null; function checkOrientAndLocation(){ if(currentRotation != window.orientation){ setOrientation(); } } function setOrientation(){ switch(window.orientation){ case 0: orient = 'portrait'; break; case 90: orient = 'landscape'; break; case -90: orient = 'landscape'; break; } currentRotation = window.orientation; document.body.setAttribute("orient",orient); setTimeout(scrollTo,0,0,1); } $(window).unload(function() { // On page unload $('.remember').each(function() { // Save each value to expire in a year $.cookie(this.id, this.value, {expires: 365}); }); $('.draggable').each(function() { // Save draggable positions var draggable = $(this); $.cookie(this.id, draggable.css('top') + '_' + draggable.css('left'), {expires: 365}); $.cookie('disp' + this.id, draggable.css('display'), {expires: 365}); }); }); $(function() { var val, pos, disp; setInterval(checkOrientAndLocation,1000); $('.remember').each(function() { var val = $.cookie(this.id); // Retrieve value for this element if (val) { this.value = val; } } ); $('.draggable').each(function() { var pos = $.cookie(this.id); // Retrieve values for this element if (pos) { pos = pos.split('_'); $(this).css({position: 'absolute', top: pos[0], left: pos[1]}); } var disp = $.cookie('disp' + this.id); if (disp) { this.style.display = disp; } } ).touch({animate: false, sticky: false, dragx: true, dragy: true, rotate: false, resort: false, scale: false }); }); There's also a file that sets the basic elements for the cookie, but the expiration date code should be in the file above. Help is very much appreciated. Hello, I am using jQuery to set a cookie and the expiration date is: Code: date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); which lasts for 1 day, but I would like the cookie to not expire for a long time, is this possible? I know google has one that like expires in 2035 or something but I would like this on to last long. What should I do? I would like to know a script for when you have a page that can expire (or cannot be open again) --- a time limit for like 24 hours. Because I'm going to make a site where people can buy website designs, they buy it off paypal, they get redirected to a site where the code is located and the site will get expired in 24 hours -- then, they wouldn't be able to open the page again. If anyone knows the script, please post it here! Thank you x 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; } }; I'm trying to find a way to display a future date in short format (as an expiration date) with Javascript. I've figured out how to do it with long format, but have no idea how to convert it and I'm not great with Javascript. Here is code that I found on another forum and modified: Code: <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <!doctype html> <head> <title>Expires</title> </head> <body> <script> var myDelayInDays=6, myDate=new Date(); myDate.setDate(myDate.getDate()+myDelayInDays); document.write('Expires: ' + myDate.toLocaleDateString()); </script> </body> </html> </body> </html> Which displays: "Expires: Monday, May 21, 2012" I would like it to display: "Expires: 05/21/2012" Can anyone help?! how long are these numbers? Code: setImageInterval("ad1", 1000, 3); I know 3 is the max, but the 1000 = how many seconds? Thanks -Tim Hi I am creating an animation based on Sam Dunn's tutorial 'Animate Curtains Opening with jQuery' ( http://buildinternet.com/2009/07/ani...g-with-jquery/ ). What i need to do is create a 2 second delay in the curtains opening in order to give enough time for an iframe, thats behind the curtains, to load it's content. Here is Sam's script : ----------------------------------------------------------------- <!-- Animate a Curtain Opening with jQuery index.html By Sam Dunn 2009 Build Internet! www.buildinternet.com --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Animate a Curtain Opening with jQuery | Build Internet</title> <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script> <script src="jquery.easing.1.3.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $curtainopen = false; $(".rope").click(function(){ $(this).blur(); if ($curtainopen == false){ $(this).stop().animate({top: '0px' }, {queue:false, duration:350, easing:'easeOutBounce'}); $(".leftcurtain").stop().animate({width:'60px'}, 2000 ); $(".rightcurtain").stop().animate({width:'60px'},2000 ); $curtainopen = true; }else{ $(this).stop().animate({top: '-40px' }, {queue:false, duration:350, easing:'easeOutBounce'}); $(".leftcurtain").stop().animate({width:'50%'}, 2000 ); $(".rightcurtain").stop().animate({width:'51%'}, 2000 ); $curtainopen = false; } return false; }); }); </script> <style type="text/css"> *{ margin:0; padding:0; } body { text-align: center; background: #4f3722 url('images/darkcurtain.jpg') repeat-x; } img{ border: none; } .leftcurtain{ width: 50%; height: 495px; top: 0px; left: 0px; position: absolute; z-index: 2; } .rightcurtain{ width: 51%; height: 495px; right: 0px; top: 0px; position: absolute; z-index: 3; } .rightcurtain img, .leftcurtain img{ width: 100%; height: 100%; } .logo{ margin: 0px auto; margin-top: 150px; } .rope{ position: absolute; top: -40px; left: 70%; z-index: 4; } </style> </head> <body> <div class="leftcurtain"><img src="images/frontcurtain.jpg"/></div> <div class="rightcurtain"><img src="images/frontcurtain.jpg"/></div> <img class="logo" src="images/buildinter.png"/> <a class="rope" href="#"> <img src="images/rope.png"/> </a> </body> </html> ----------------------------------------------------------------- Any help will be greatly appreciated. Thanks. Jet. I need to develop this feature for a charity site displays a number that counts up 1 every 15 seconds. This is to show how many times a kid is abused in this country. I've been researching this and haven't been able to find anything helpful yet. I figured this piece of code was a good start: function doSomething() { setTimeout('doSomething()',15000); } Hey guys..... I'm wondering how you go about getting a script to run every 30 seconds for example? Thanks a lot! I want a code that changes text (of maybe a div) after 10 seconds. So like i could have some text and a link on my page then after 10 seconds it will change to something else and i can make it change to as many things as i want. Then when it goes threw them all it starts over. THANKS Can someone help me to modify this JS to close if user is no longer hovering over child link after 5 seconds? Code: $(document).ready(function () { $('#menuHolder ul li a').mouseover(function (event) { if (this == event.target) { $(this).parent().toggleClass('clicked'); if ($(this).parent().attr('class').indexOf('clicked') != -1) { $(this).siblings('ul').animate({"top": "35px"}, {queue:false,duration:(500)}, "swing"); } else { $(this).siblings('ul').animate({"top": "0px"}, {queue:false,duration:(500)}, "swing"); } $(this).parent().siblings().removeClass('clicked').find('ul').animate({"top": "0px"}, {queue:false,duration:(500)}, "swing"); } }) $('#menuHolder ul li:not(:has(ul)) a').mouseout(function (event) { if (this == event.target) { $(this).parent().toggleClass('clicked'); $(this).parent().siblings().removeClass('clicked').find('ul').animate({"top": "0px"}, {queue:false,duration:(500)}, "swing"); } }); }); </script> topic has been answered
Hello, I found this code, written by Philip M on another thread of this forum: Code: <div id = "adsdiv">This ad will close in <span id = "closingtimer">31</span> seconds </div> <script type="text/javascript"> function closeMyAd() { document.getElementById("adsdiv").style.display = "none" ; } var seconds = 31; function display() { seconds --; if (seconds < 1) { closeMyAd(); } else { document.getElementById( "closingtimer" ).innerHTML = seconds ; setTimeout("display()", 1000); } } display(); </script> I need to reverse the script to show a div after 30 seconds, not to hide it. Can you please help me? Thank you. Hi people, I'm building an online psychological experiment in which I need to display an image for 5 seconds on the screen. Does anyone has a simple script for that? I found some script that does image rotation but its not exactly what I am looking for. First, the rotation of the images doesn't stop and second I don't need something so elaborate. This is the script that I found. Code: <SCRIPT LANGUAGE="JavaScript"> <!-- var dimages=new Array(); var numImages=2; for (i=0; i<numImages; i++) { dimages[i]=new Image(); dimages[i].src="images/image"+(i+1)+".jpg"; } var curImage=-1; function swapPicture() { if (document.images) { var nextImage=curImage+1; if (nextImage>=numImages) nextImage=0; if (dimages[nextImage] && dimages[nextImage].complete) { var rel="nofollow" target=0; if (document.images.myImage) rel="nofollow" target=document.images.myImage; if (document.all && document.getElementById("myImage")) rel="nofollow" target=document.getElementById("myImage"); // make sure target is valid. It might not be valid // if the page has not finished loading if (target) { target.src=dimages[nextImage].src; curImage=nextImage; } setTimeout("swapPicture()", 5000); } else { setTimeout("swapPicture()", 500); } } } setTimeout("swapPicture()", 5000); //--> </SCRIPT> Thanks I have a welcome message on my site and would like to automatically hide it after several seconds. I can't find anything usable after searching over the internet. Can anyone help? Thanks in advance. This is the script to hide the div (adsdiv) Its working on all browser except IE Code: <script type="text/javascript"> function closeMyAd() { document.getElementById("adsdiv").style.display = "none" ; } var milisec=0 var seconds=30 function display(){ if (milisec<=0){ milisec=9 seconds-=1 } if (seconds<=-1){ milisec=0 seconds+=1 } else milisec-=1 if( seconds > 0 ) { document.getElementById( "closingtimer" ).innerHTML = seconds ; setTimeout("display()",100) ; }else{ closeMyAd() ; } } if( document.getElementById( "closingtimer" ) ) display() ; </script> Hi, I've built a sorting table for what will (hopefully) be a leaderboard for Forza 4. I have the table sorted properly and my current script (within the table) adds up all three T columns and brings the total to 72.001 as expected, woot! Where I'm stuck is converting the 72.001 to 1 minute 12.001 seconds (1:12.001) and I need to have it include the thousandths since the game gives that information. Once I get the JS going for the top row I can copy down Any advice on converting this is greatly appreciated and I Bolded the JS I'm referring to: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Forza Motorsport 4 FRS Leaderboard</title> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <script type="text/javascript" src="sortable.js"></script> <style> table { text-align: left; font-size: 12px; font-family: verdana; background: #c0c0c0; } table thead { cursor: pointer; } table thead tr, table tfoot tr { background: #c0c0c0; } table tbody tr { background: #f0f0f0; } td, th { border: 1px solid white; } </style> </head> <body> <table cellspacing="3" cellpadding="4" class="" id="myTable"> <thead> <tr> <th class="c1">DRIVER</th> <th class="c2">1</th> <th class="c2">2</th> <th class="c2">3</th> <th class="c6">TOTAL</th> <th class="c5">DIVISION</th> <th class="c7">CONTROLLER</th> </tr> </thead> <tbody> <tr id="SubTable1" class="r1"> <td class="c1">Driver Name 1</td> <td class="c2">12.000</td> <td class="c2">24.000</td> <td class="c2">36.001</td> <script language="javascript" type="text/javascript"> var tds = document.getElementById('SubTable1').getElementsByTagName('td'); var totalTime = 0.000; for(var i = 0; i < tds.length; i ++) { if(tds[i].className == 'c2') { totalTime += isNaN(tds[i].innerHTML) ? 0 : parseFloat(tds[i].innerHTML); } } document.getElementById('SubTable1').innerHTML += totalTime; </script> <td class="c5">1</td> <td class="c7">Wheel</td> </tr> <tr class="r2"> <td class="c1">Driver Name 2</th> <td class="c2">12.100</th> <td class="c3">24.100</th> <td class="c4">36.100</th> <td class="c6">00.000</th> <td class="c5">2</th> <td class="c7">Gamepad</th> </tr> <tr class="r2"> <td class="c1">Driver Name 3</th> <td class="c2">12.200</th> <td class="c3">24.200</th> <td class="c4">36.200</th> <td class="c6">00.000</th> <td class="c5">3</th> <td class="c7">Wheel</th> </tr> <tr class="r2"> <td class="c1">Driver Name 4</th> <td class="c2">12.300</th> <td class="c3">24.300</th> <td class="c4">36.300</th> <td class="c6">00.000</th> <td class="c5">4</th> <td class="c7">Gamepad</th> </tr> <tr class="r2"> <td class="c1">Driver Name 5</th> <td class="c2">12.400</th> <td class="c3">24.400</th> <td class="c4">36.400</th> <td class="c6">00.000</th> <td class="c5">5</th> <td class="c7">Wheel</th> </tr> </tbody> </table> <script type="text/javascript"> var t = new SortableTable(document.getElementById('myTable'), 100); </script> </body> </html> Oh, and any help on centering the result in the cell (72.001) would be excellent too, thank you. Jerome know why does window.open take atlst 2-3 seconds, only to open even a simple blank page? in my code, i load a page on window.open and increament a counter . but it takes 2-3 seconds and in meantime user is closing new opened window, so counter val is not correct. i cannt stop user from closing new window when it is loading, which is long given what it is doing-- see sample here from Microsoft site - simple window open taking 2-3 secs.. looks to be long. http://samples.msdn.microsoft.com/wo...ew/wind_02.htm thank in advance Hi, I'm trying to adapt a script I found online for a timer that goes in my webpage. The timer counts from 30 seconds down to 0. I've adapted it so that when it reaches 0 it goes back up to 30, but I was wondering how can I make it so that after a certain number of loops through the timer, it executes some other (non-JS) code instead of just doing the timer forever? I've introduced a variable t to try to do this, but when I put in if(t>1) {break} at the bottom, the timer doesn't work properly anymore (it doesn't display a number in the box). Thanks a lot for any help . <script> <!-- // var t=0 var milisec=0 var seconds=30 document.counter.d2.value='30' function display(){ if (milisec<=0){ milisec=9 seconds-=1 } if (seconds<=-1){ milisec=0 seconds=30 t+=1 } else milisec-=1 document.counter.d2.value=seconds+"."+milisec setTimeout("display()",100) } display() --> </script> |