[JS + jquery] jquery-cookie ํ”Œ๋Ÿฌ๊ทธ์ธ

2021. 11. 3. 10:33ใ†HTML + CSS + JS/JS ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

728x90

1. jquery-cookie ํ”Œ๋Ÿฌ๊ทธ์ธ 

jquery-cookie ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฟ ํ‚ค ๋งŒ๋ฃŒ์ผ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

โ— ์ฟ ํ‚ค

์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž์˜ ์†Œ๋Ÿ‰์˜ ๋ธŒ๋ผ์šฐ์ € ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค. 

์ฟ ํ‚ค๋Š” ํ•˜๋‚˜์˜ ๋„๋ฉ”์ธ๋‹น ์ตœ๋Œ€ 20๊ฐœ๊นŒ์ง€ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ  ์ตœ๋Œ€ 4kb๊นŒ์ง€ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ '3์ผ๋™์•ˆ ์—ด์ง€ ์•Š๊ธฐ', '์ตœ๊ทผ์— ๋ณธ ์ƒํ’ˆ' ๋“ฑ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

https://cdnjs.com/libraries/jquery-cookie

 

jquery-cookie - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers

A simple, lightweight jQuery plugin for reading, writing and deleting cookies. - Simple. Fast. Reliable. Content delivery at its finest. cdnjs is a free and open-source CDN service trusted by over 12.5% of all websites, serving over 200 billion requests ea

cdnjs.com

์—ฌ๊ธฐ์„œ jquery-cookie ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋‹ค์šด๋ฐ›๋Š”๋‹ค. 

 

 

2. jquery-cookie ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ๋ฒ• 

$.cookie("์ฟ ํ‚ค์ด๋ฆ„", "์ฟ ํ‚ค๊ฐ’", {expire:๋งŒ๋ฃŒ์ผ, path: "์ €์žฅ ๊ฒฝ๋กœ"});

$.cookie("์ฟ ํ‚ค์ด๋ฆ„"); -> "์ฟ ํ‚ค ์ด๋ฆ„"์œผ๋กœ ์ €์žฅ๋œ ์ฟ ํ‚ค์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

$.removeCookie("์ฟ ํ‚ค ์ด๋ฆ„");  -> "์ฟ ํ‚ค ์ด๋ฆ„"์œผ๋กœ ์ €์žฅ๋œ ์ฟ ํ‚ค๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. 

 

3์ผ๋™์•ˆ ์ด ์ฐฝ ์—ด์ง€ ์•Š๊ธฐ๋ฅผ ์ฒดํฌํ•˜๊ณ  ๋‹ซ์œผ๋ฉด ์ฟ ํ‚ค๊ฐ€ ์ƒ์„ฑ๋˜์–ด 3์ผ๋™์•ˆ ์ด ํŒ์—…์ด ์•ˆ๋ณด์ด๊ฒŒ ํ•œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์ฒดํฌ์•ˆํ•˜๊ณ  ๋‹ซ์œผ๋ฉด ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ํŒ์—…์„ ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> Plugin </title>
<style>
	*{margin: 0;padding: 0;}
	body{font-size: 12px;}
	input{vertical-align: middle;}
	#notice_wrap{
		width: 300px;
		position: absolute;
		left: 30px;
		top: 30px;
		box-shadow:0 0 8px #000;
	}
	.closeWrap{
		background-color: #000;
		color:#fff;
		text-align: right;
		padding:5px 10px;
	}
	.closeWrap button{
		margin-left: 20px;
		cursor:pointer;
	}
</style>
<script src="js/jquery.js"></script>
<script src="js/jquery.cookie.js"></script>
<script>
$(function() {
	if($.cookie("popup") == "none") {
		$("#notice_wrap").hide();
	}

	var $expChk = $("#expiresChk");
	$(".closeBtn").on("click", closePop);

	function closePop(){
		if($expChk.is(":checked")){
			$.cookie("popup", "none", {expires : 3,path:"/"});
		}

		$("#notice_wrap").fadeOut("fast");
	}
});
</script>
</head>
<body>
	<div id="notice_wrap">
		<img src="images/pic_1.jpg" alt="๊ณต์ง€์‚ฌํ•ญ">
		<p class="closeWrap">
			<input type="checkbox" name="expiresChk" id="expiresChk">
			<label for="expiresChk">3์ผ ๋™์•ˆ ์ด ์ฐฝ ์—ด์ง€์•Š๊ธฐ</label>
			<button class="closeBtn">๋‹ซ๊ธฐ</button>
	</p>
	</div>  
</body>
</html>

์ฟ ํ‚ค๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ ๋‹ค์‹œ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ €๊ฑธ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ํŒ์—…์„ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90