2013-07-04 2 views
-1

안녕하세요. 브라우저 용 writting 플러그인이 새로 생겼습니다. 내 요구 사항은 URL의 쿠키 값을 읽으려면 샘플 javascript 플러그인 코드를 작성해야하며 모든 브라우저를 지원해야한다는 것입니다. 방금 시스템의 쿠키 파일에서 직접 읽을 수 없으며 Chrome 용 sqlite 데이터베이스를 통해 처리해야한다는 것을 알았습니다. 진행 방법이 도움이 될 것입니다.브라우저에서 URL의 쿠키 값을 읽는 Javascript 플러그인

+0

무엇을 이미 시도 했습니까? – mishik

+0

@mishik : w3 schools.Below의 샘플 코드를 사용해 보았습니다. function getCookie (c_name) { var c_value = document.cookie; var c_start = c_value.indexOf (""+ c_name + "="); if (c_start == -1) { c_start = c_value.indexOf (c_name + "="); } if (c_start == -1) { c_value = null; } else { c_start = c_value.indexOf ("=", c_start) +1; var c_end = c_value.indexOf (";", c_start); if (c_end == -1) { c_end = c_value.length; } c_value = unescape (c_value.substring (c_start, c_end)); } 경고 (c_value) return c_value; } 그러나 이것이 어떻게 도움이 될지 잘 모르겠다. –

답변

0

이 코드를 사용하십시오.

$.cookie('the_cookie', 'the_value'); 
//Create expiring cookie, 7 days from then: 
$.cookie('the_cookie', 'the_value', { expires: 7 }); 

//Create expiring cookie, valid across entire page: 
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); 

//Read cookie 
$.cookie('the_cookie'); // => 'the_value' 
$.cookie('not_existing'); // => null 

//Delete cookie by passing null as value: 
$.cookie('the_cookie', null); 

// Creating cookie with all availabl options 
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com', 
secure: true, raw: true }); 

https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

+0

jQuery 태그가 보이지 않는다. – mishik

+0

jquery가 아닌 javascript를 사용하여 개발해야한다. –

0

쿠키 문자열 형식으로

몇 가지 예 document.cookie를 통해 액세스 할 수있는이 URL에서 다운로드 JQuery와 쿠키() : 당신이 그것을 읽어 갔다

document.cookie = "YOUR_KEY_1=YOUR_VALUE_1;YOUR_KEY_2=YOUR_VALUE_2;..." 

그리고 쿠키를 설정할 때 (쿠키로 쿠키)

console.log(document.cookie) 
/* "YOUR_KEY=YOUR_VALUE; expires=EXPIRATION_DATE; path=WHERE_YOUR_COOKIE_WILL_BE_ACCESSIBLE" 
eg. 
    user=599a9182df1...; expires=Tue, 15 Jul 2014 14:06:12 GMT; path=/logged_in 
    So, the cookie "user" as the value 599a9182df1... (Some SHA1 example) 
    It expires in 1 year (Won't be accessible if not renewed) 
    Is only accessible under /logged_in 
*/ 

두 가지 기능 (가져 오기 및 설정)과 사용 방법 예를 만들었습니다.

// Definition of the cookie duration 
var my_cookie_duration_hash = { 
    days:  1, 
    hours: 0, 
    minutes: 0, 
    seconds: 0 
}; 

// Definition of the cookies key/value pair 
var my_cookies = { 
    key1: 'value1=', 
    key2: 'value2' 
}; 


// Let's set some cookies 
set_cookies(my_cookies, my_cookie_duration_hash, '/'); 

// Let's get our cookies 
window.alert(get_cookies()['key1']); 


/***************************************************************** 
    Cookie home made functions 
*****************************************************************/ 

// Get the cookie key/value in JSON format 
function get_cookies() { 
    var cookies = {}; 

    cookies_str = document.cookie.split(';'); 

    // Processing the cookies 
    for(var i = 0 ; i < cookies_str.length ; i++) { 
    cookie = cookies_str[i]; 

    var cookie_match = cookie.match(/^([^=]*)=(.*)$/); 

    // If The cookie exist, we get it back in a JSON 
    if(cookie_match) { 
     cookies[cookie_match[1]] = cookie_match[2]; 
    } 
    } 

    // We return the cookies in a JSON format 
    return cookies; 
} 

// Set the cookies 
// arg(0) : Cookies values 
//  Format : Some { key: value } JSON object 
// arg(1) : Cookie duration 
function set_cookies() { 
    cookies   = arguments[0] || {}; 
    cookie_duration = arguments[1] || {}; 
    cookie_domain = arguments[2] || '/'; 

    if(typeof(cookie_duration) === 'object') { 
    my_cookie_duration_int = (cookie_duration.days || 0) * 24 * 60 * 60 * 1000; 
          + (cookie_duration.hours || 0) * 60 * 60 * 1000; 
          + (cookie_duration.minutes || 0) * 60 * 1000; 
          + (cookie_duration.seconds || 0) * 1000; 
    } else if(typeof(cookie_duration) === 'number') { 
    my_cookie_duration_int = cookie_duration; 
    } else if(typeof(cookie_duration) === 'undefined') { 
    my_cookie_duration_int = 0; 
    } else if(typeof(cookie_duration) !== 'undefined') { 
    console.error(typeof(cookie_duration) +' is not a recognize type for the cookie duration.'); 
    } 

    // Calculation of the cookie end of validity 
    var date  = new Date(); 
    var end_date = date.getTime() + my_cookie_duration_int; 
    date.setTime (end_date); 

    var my_cookie_expiration_date = date.toGMTString(); 

    // Processing of the cookie 
    for(var my_cookie in cookies) { 
    new_cookie = my_cookie +"="+ cookies[my_cookie] 
       + "; expires="+ my_cookie_expiration_date 
       + "; path="+  cookie_domain; 

    // Definition of the cookies 
    document.cookie = new_cookie; 
    } 
} 

나는 그것이 도움이되기를 바랍니다 (IE6 & 파이어 폭스 (22)와 호환).

관련 문제