[3] Development/MVC(ASP.NET with C#)
View 단에서 Cookie값 읽어오기. - jquery.cookie.js
코볼
2023. 3. 3. 09:52
728x90
반응형
SMALL
1. 일단 Layout 파일의 head tag 사이에 .js 파일을 참조 합니다. (단일 페이지로 작업 한다면 그 페이지에 넣어주면 됨)
<head><script src="~/js/jquery.cookie.js"></script></head>
2. 아래 함수를 넣고 사용 하면 됩니다.
function findCookieValue(jsCookieName, jsCookieItemName) {
var start = $.cookie(jsCookieName).indexOf(jsCookieItemName);
var jsCookieItemValue = '';
if (start != -1) {
start += jsCookieItemName.length;
var end = $.cookie(jsCookieName).indexOf('&', start);
if (end == -1) end = $.cookie(jsCookieName).length;
jsCookieItemValue = $.cookie(jsCookieName).substring(start + 1, end);
}
return jsCookieItemValue;
}
3. 사용 예시
var jsCookieItemValue = findCookieValue('Cookie', '사용자명');
$("#div사용자명").html("이름: " + jsCookieItemValue);
728x90
반응형
LIST