paulmo Posted February 1, 2010 Share Posted February 1, 2010 How to capitalize name cookie (first name only)? function checkCookie(){name=getCookie('name');name=CapitalizeNames('name'); //this doesn't workname=getCookie(CapitalizeNames('name')); //doesn't work either Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2010 Share Posted February 1, 2010 Did you declare a CapitalizeNames() function anywhere? Link to comment Share on other sites More sharing options...
paulmo Posted February 1, 2010 Author Share Posted February 1, 2010 I'm sorry I thought that was a built-in function. What is the built-in solution? Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2010 Share Posted February 1, 2010 There's no native Javascript function that will capitalize a word, but you can use a few string functions together to do it. This is the Javascript String Object Reference Link to comment Share on other sites More sharing options...
paulmo Posted February 2, 2010 Author Share Posted February 2, 2010 Thanks; I see how to do that with a form input, but how about with a body onLoad alert box to get name cookie? Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2010 Share Posted February 2, 2010 Is there even a cookie on your site?Did you declare the getCookie() function? Link to comment Share on other sites More sharing options...
paulmo Posted February 2, 2010 Author Share Posted February 2, 2010 Yes in development (not live) I've got first name cookie. If I don't capitalize it reads "Hello paul!" which is pretty amateurish. I want "Hello Paul!" Here's code if you might please advise where to modify: function getCookie(c_name){if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)); } }return ""}function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}function checkCookie(){name=getCookie('name');//name=CapitalizeNames('name');if (name!=null && name!="") { alert('Hello '+name+'!'); }else { name=prompt('Please introduce yourself by entering your name or nickname:',""); if (name!=null && name!="") { setCookie('name',name,365); } }} Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2010 Share Posted February 2, 2010 All you really need to do is create a capitalize() function and use it. I gave you the reference to string functions before, here is how to use them. function capitalize(str) { firstLetter = str.substr(0,1); remainingString = str.substr(1); return firstLetter.toUpperCase() + remainingString} To use the function, you would just do this: capitalize(name); Link to comment Share on other sites More sharing options...
paulmo Posted February 2, 2010 Author Share Posted February 2, 2010 Thank you for that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.