antonyfal Posted November 4, 2011 Share Posted November 4, 2011 Hi, I know there are hundreds of examples on the net, but im just not grasping it!! all the examples i come across have expiring time, and a whole lot of other insignificant to me stuff (at the moment).. im trying to pull the value from this cookie that i set. //for this example lets say: //{THEEMAILNAME} = johndoe; //{THEEMAILDOMAIN} = doesnet.com; //so the email im trying to pull is: johndoe@doesnet.com from the following cookie. //PS: this is all done on a .html file. <script type="text/javascript"> document.cookie ="mailaddress={THEEMAILNAME}@{THEEMAILDOMAIN}; path=/"; </script> Please can some one show me how to do this from this example? with a simple explanation.. Best regards Antony Quote Link to comment https://forums.phpfreaks.com/topic/250468-how-to-retrieve-values-of-a-cookie/ Share on other sites More sharing options...
antonyfal Posted November 5, 2011 Author Share Posted November 5, 2011 I haven't had a response :'( Here are some script to get the value that i found on the net: How/which can i apply to my request above? //one version of the cookie script: function ReadCookie(cookiename) { var numOfCookies = document.cookie.length; var nameOfCookie = cookiename + "="; var cookieLen = nameOfCookie.length; var x = 0; while (x <= numOfCookies) { var y = (x + cookieLen); if (document.cookie.substring(x, y) == nameOfCookie) return (extractCookieValue(y)); x = document.cookie.indexOf(" ", x) + 1; if (x == 0){ break; } } return (null); } //another version: <script type="text/javascript"> function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies.substr(0,ARRcookies.indexOf("=")); y=ARRcookies.substr(ARRcookies.indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } } // and another version>.. comes with own explanations but i dont understand them.. // this fixes an issue with the old method, ambiguous values // with this test document.cookie.indexOf( name + "=" ); function Get_Cookie( check_name ) { // first we'll split this cookie up into name/value pairs // note: document.cookie only returns name=value, not the other components var a_all_cookies = document.cookie.split( ';' ); var a_temp_cookie = ''; var cookie_name = ''; var cookie_value = ''; var b_cookie_found = false; // set boolean t/f default f for ( i = 0; i < a_all_cookies.length; i++ ) { // now we'll split apart each name=value pair a_temp_cookie = a_all_cookies.split( '=' ); // and trim left/right whitespace while we're at it cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ''); // if the extracted name matches passed check_name if ( cookie_name == check_name ) { b_cookie_found = true; // we need to handle case where cookie has no value but exists (no = sign, that is): if ( a_temp_cookie.length > 1 ) { cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); } // note that in cases where cookie is initialized but no value, null is returned return cookie_value; break; } a_temp_cookie = null; cookie_name = ''; } if ( !b_cookie_found ) { return null; } } Quote Link to comment https://forums.phpfreaks.com/topic/250468-how-to-retrieve-values-of-a-cookie/#findComment-1285196 Share on other sites More sharing options...
antonyfal Posted November 5, 2011 Author Share Posted November 5, 2011 Hi, i tried a couple of attempts but im not getting any luck! here is one of the attempts, why is it not working? <script type="text/javascript"> function getCookie(mailaddress) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies.substr(0,ARRcookies.indexOf("=")); y=ARRcookies.substr(ARRcookies.indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==mailaddress) { var thisemail=y; } var email_split=thisemail.split("@"); var email_name=email_split[0]; var email_domain=email_split[1]; } } </script> <input size="20" class="text" name="_username" type="text" value="<myCookie: var email_name />" readonly> Quote Link to comment https://forums.phpfreaks.com/topic/250468-how-to-retrieve-values-of-a-cookie/#findComment-1285215 Share on other sites More sharing options...
Amit20 Posted November 7, 2011 Share Posted November 7, 2011 Check out this code. It'll Help! function getCookie(cookieName){ if(document.cookie.length>0){ var begin = document.cookie.indexOf(cookieName+"="); if(begin!=-1){ begin += cookieName.length+1; var end = document.cookie.indexOf(";", begin); if (end == -1){ end = document.cookie.length; } return unescape(document.cookie.substring(begin,end)); } return null; } } Quote Link to comment https://forums.phpfreaks.com/topic/250468-how-to-retrieve-values-of-a-cookie/#findComment-1285805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.