JavaScript - Cookie -- Learning To Read Problem
Hi all
How should I be doing this? I am trying to 'learn' how to set more than one value in a cookie. Then get them back out. Obviously i'm on the wrong track. These are my 'test' values: Code: var val = document.getElementById("yrname").value; //peterPan var val1 = document.getElementById("password").value; //tinkerBell //send to create cookie factory createCookie("inbed", val + '~' + val1, "1"); Then from url : alert(cookie) I get: inbed=peterPan~tinkerBell So far so good (well what I expected anyway) now using this function I completely lose tinkerBell. she was inbed with peterPan. Code: function readCookie(name) //name is passed in { var cookies = document.cookie.split(/~/g); //split at ~ //alert(cookies); //results: //cookies: inbed=peterPan,tinkerBell //cookies[0]: mytestcookie=peterPan // cookies[1]: tinkerBell //the last sighting var arg = name + "="; for ( var c = 0; c < cookies.length; ++c ) //iterate through the cookie { var ck = cookies[c]; //cookie array into an var //alert(ck); //inbed=peterPan (no tinkerBell -- not what I expected! //basically i'm now lost!!! //I don't get what happened to tinkerBell . if ( ck.indexOf(arg) == 0 ) //if the array at the index of arg 'name=' equal to position 0. { var temp = ck.split(/=/); //split the cookie array at = //alert(temp); //inbed=peterPan //alert(temp[0]); //inbed //alert(temp[1]); //peterPan , return decodeURI(temp[1]) //bring back array 1 } } return ""; } help on how I should be doing this much appreciated LT Similar TutorialsUnfortunately, this isn't standard cookie stuff (I don't think). I have a compiled .exe loader for a program which includes an .html page which records the input of a textbox to a cookie. That much works OK. But reading it back isn't working. It seems the path is: ~~local~~/C:/Users/Terry/AppData/Local/Temp/td9/FrontPage%20Webs/1001(FINAL)(v1.2)/loader/ i.e. a temp dir related to the directory from where the .exe file was created. What I need is for the cookie to be available everywhe ~~local~~/ Is anyone able to shed some light? Adding ";path=/"; isn't working. I guess it's something to do with being compiled? 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; } }; Im trying to learn how to code but i'm not getting it can anyone help me with java? <3 -ButterFly<3 I am currently trying to learn the <form> object. Now I have done some things within <form> like setting up text fields with submit buttons, etc.. But now on this part of the course it is showing properties I think, within the decleration of the <form> such as shown below Code: <FORM METHOD="POST" ACTION="/cgi-bin/correct.pl" onSubmit="return false;"> Could someone explain to me what that ACTION="/cgi-bin/corrct.pl" is for along with the onSubmit="return false;" ?? I'd like an explanation of the properties of <form> if you could please. So should I be learning javascript, what am I going to achieve by understanding this language? My background: Designed a website for my wifes shop: http://www.eden-flowers.co.uk Enjoyed the html and css design side of things, then we decided to add a shoppping cart, to see if we could sell a few products online. Used opencart by the way, which I thought was a good free product and reasonably easy to implement. Having looked deeper into the opencart noticed that it used PHP, so I'm thinking I would like to understand this better. And here I am now trying to understand Javascript purely because I use w3schools alot for reference and their next logical step seemed to be Javascript. I'm rambling, but if you care to respond to this very open question, then pls feel free to voice your opinion/suggestions. Code: <HTML> <HEAD> <SCRIPT TYPE="text/javascript"> //Define Functions function student(name,age,mother) { this.name = name; this.age = age; this.mother = mother; this.displayProfile = displayProfile; } function grade(math,english,science) { this.math = math; this.english = english; this.science = science; } function displayProfile() { document.writeln("Name: " + this.name + "); document.writeln("Age: " + this.age + "); document.writeln("Mother's name: " + this.mother + "); document.writeln("Math Grade: " + this.grade.math + "); document.writeln("English Grade: " + this.grade.english + "); document.writeln("Science Grade: " + this.grade.science + "); } </SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="text/javascript"> { student1 = new student("John",18,"Diane"); johngrade = new grade(100,80,90); student1.displayProfile(); } </SCRIPT> </BODY> </HTML> Alright, I am learning javascript and in the middle of experimenting with functions, I don't see why this does not work? In order to learn "cool" javascript such as rollovers that you find on websites today; what would any of you guys reccomend me doing. I want to learn how to make my webpage interactive and i know what i want to do; i just dont know how to do it any suggestions are welcome; thank you Hi, I'm in the process of learning JavaScript. I picked up a "JavaScript for dummies" book at the library and it wasn't going too well. It was poorly written and made it difficult to understand things in the later chapter. What is the best online resource to learning JavaScript? Preferrably one that is good at explaining format ( I often find myself wondering if certain aspects in my script need ()'s or a "=" sign). For example, HTML has tags and that is what forms the mark up. I know JavaScript has multiple aspects (functions, objects), but I want an online resourcethat properly explains the format of these aspects. Thank you Hello everyone, I am currently going through the process of learning JavaScript and I am enjoying it. I also know enough about HTML & CSS and I am learning more and more each day. I started doing this because I figured that finding a technical co-founder is extremely hard and I have always been interested in web and app development, so why not learn... I was wonding whether someone could direct me? I want to be able to develop websites such as scan.me or gumroad.com (similar concepts). Which code stack are these websites generally made up of (e.g php, HTML CSS and java). I'm hoping to develop my own platform/concept in the future but I am just trying to get a general idea of the direction I am going down. Thanks, Paul Hi there! I'm obviously new to the forums here and don't really know if this is the right place to get help. I've made a small game in an HTA file using JavaScript and HTML (don't laugh, this was a serious learning project for me). The problem I'm having is that, after running the game for a while (having played, like to level 10 say), the HTA begins to slow down HORRIBLY. By level 10, it's almost unplayable... it takes like 3 seconds to register a button click. My messy code is below (yes, it's the entire game... I know it's huge). I don't really care that the math is horrible in calculating hit points and damage and whatnot. All I want to know is, why the hell does this slow down? Any help would be greatly appreciated. (obviously, rename the text file to .hta or .html to load it) Hello all. I have been taking a JavaScript course on Udemy that while has been a very good course as far as I can tell, it has been short on practical exercises. Lots of examples, lots of showing different concepts and methods, etc, just short on the reinforcing and confidence building exercises. Thus, the point to my post here. I wish there were some kind of web developing club here locally but alas if you don't hunt, fish, drink or chew tobacco you are kind of left out, LOL. Well its not that bad, I make fun of it, but sometimes it seems so. At any rate, I'm looking for some kind of resource or something I can participate in that will help me use what I am learning and help me build some experience, etc. I want to get past the 'noob' stage as quickly as I can and as far as I know the quickest way to do that is to start coding even if its really simple stuff and working to more complicated coding. So, does anyone know of such resources? Thanks in advance for your time and help. Ok guys, i am starting from he http://www.javascriptkit.com/javatutors/primer5.shtml Code: <script type="text/javascript"> var example example=document.lastModified document.write("This page was last modified: "+example) </script> I am having trouble understanding an actual situation which may require the above coding, can anyone give an example please? I have no js experience or coding experience of any kind other than the last few hours of reading a book called 'object-oriented javascript', and all was well until loops. firstly, this is how he teaches the while loop... var i = 0; while (i <10) {i++;} which results in 9 we move on to for loops... var res =''; for( var i = 0; i <10; i++) {res += 'sometext' ;} which repeats, but I'd like to be able to make that into a sequence of numbers... 123456789. how do I do that? thanks. Hi, as the title states, I'm trying to learn some basic javascript, more particularly, jQuery. I HATE having to "allow" active content to run on my IE browser. Is there a way to allow active content permanantly? Instead of having to click allow each time I refresh or reload my test pages? Also, IE keeps freezing or crashing when I run pages with scripts on them. Is this just my browser? or something to do with the active content permissions? Trying to google my questions, but no luck yet. 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 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 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! 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> Hello I have below script i need add cookie to it . disable script untill cookie is not expired can you please help me <script language=javascript type="text/javascript"> <!-- Hide script from old browsers //new window script function jshow() {window.open("http://www.google.com","blank")} // End hiding script from old browsers --> </script> 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. |