1/* 2Script Name: Javascript Cookie Script 3Author: Public Domain, with some modifications 4Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php 5Version 1.1.2 6Last Update: 5 November 2009 7 8Changes: 91.1.2 explicitly declares i in Get_Cookie with var 101.1.1 fixes a problem with Get_Cookie that did not correctly handle case 11where cookie is initialized but it has no "=" and thus no value, the 12Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks 131.1.0 fixes a problem with Get_Cookie that did not correctly handle 14cases where multiple cookies might test as the same, like: site1, site 15This library is distributed in the hope that it will be useful, 16but WITHOUT ANY WARRANTY; without even the implied warranty of 17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 18*/ 19 20// this fixes an issue with the old method, ambiguous values 21// with this test document.cookie.indexOf( name + "=" ); 22 23// To use, simple do: Get_Cookie('cookie_name'); 24// replace cookie_name with the real cookie name, '' are required 25function Get_Cookie( check_name ) { 26 // first we'll split this cookie up into name/value pairs 27 // note: document.cookie only returns name=value, not the other components 28 var a_all_cookies = ''; 29 try { 30 a_all_cookies = document.cookie.split( ';' ); 31 } catch(err) { 32 }; 33 var a_temp_cookie = ''; 34 var cookie_name = ''; 35 var cookie_value = ''; 36 var b_cookie_found = false; // set boolean t/f default f 37 var i = ''; 38 39 for ( i = 0; i < a_all_cookies.length; i++ ) 40 { 41 // now we'll split apart each name=value pair 42 a_temp_cookie = a_all_cookies[i].split( '=' ); 43 44 45 // and trim left/right whitespace while we're at it 46 cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ''); 47 48 // if the extracted name matches passed check_name 49 if ( cookie_name == check_name ) 50 { 51 b_cookie_found = true; 52 // we need to handle case where cookie has no value but exists (no = sign, that is): 53 if ( a_temp_cookie.length > 1 ) 54 { 55 cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); 56 } 57 // note that in cases where cookie is initialized but no value, null is returned 58 return cookie_value; 59 break; 60 } 61 a_temp_cookie = null; 62 cookie_name = ''; 63 } 64 if ( !b_cookie_found ) 65 { 66 return null; 67 } 68} 69 70/* 71only the first 2 parameters are required, the cookie name, the cookie 72value. Cookie time is in milliseconds, so the below expires will make the 73number you pass in the Set_Cookie function call the number of days the cookie 74lasts, if you want it to be hours or minutes, just get rid of 24 and 60. 75 76Generally you don't need to worry about domain, path or secure for most applications 77so unless you need that, leave those parameters blank in the function call. 78*/ 79function Set_Cookie( name, value, expires, path, domain, secure ) { 80 // set time, it's in milliseconds 81 var today = new Date(); 82 today.setTime( today.getTime() ); 83 // if the expires variable is set, make the correct expires time, the 84 // current script below will set it for x number of days, to make it 85 // for hours, delete * 24, for minutes, delete * 60 * 24 86 if ( expires ) 87 { 88 expires = expires * 1000 * 60 * 60 * 24; 89 } 90 //alert( 'today ' + today.toGMTString() );// this is for testing purpose only 91 var expires_date = new Date( today.getTime() + (expires) ); 92 //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only 93 94 document.cookie = name + "=" +escape( value ) + 95 ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString() 96 ( ( path ) ? ";path=" + path : "" ) + 97 ( ( domain ) ? ";domain=" + domain : "" ) + 98 ( ( secure ) ? ";secure" : "" ); 99} 100 101// this deletes the cookie when called 102function Delete_Cookie( name, path, domain ) { 103 if ( Get_Cookie( name ) ) document.cookie = name + "=" + 104 ( ( path ) ? ";path=" + path : "") + 105 ( ( domain ) ? ";domain=" + domain : "" ) + 106 ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; 107}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.