js 오늘하루보이지않기 레이어 팝업 쿠키적용

Sora Kim·2022년 11월 4일
1

JS / jquery

목록 보기
9/12

참고사이트

https://zero-jin.tistory.com/13

html

<div class="mainPop" id="mainPop" style="display:none;">
	<div class="mainPop_txt">
		<h3> content </h3>'
    </div>
    <div class="mainPop_check">
		<input  type="checkbox" id="todayView" name="todayView" /><label for="todayView" class="checkbox">오늘 하루 열지 않기</label>
		<a href="javascript:closeMainPop(1);" class="popup_closeBtn"><i class="ti-close"></i></a>
	</div>
</div>
        

JS

(제이쿼리 cdn 생략)
<script>
$(document).ready(function(){
	if(getCookie("mainPop1") != 'Y'){
		$("#mainPop").show();
	}
});
function setCookie(name, value, expiredays) {
		var today = new Date();
		today.setDate(today.getDate() + expiredays);
		document.cookie = name + '=' + escape(value) + '; path=/; expires = '+ today.toGMTString() + ';';
}
function getCookie(name){
		var obj = name + '=';
		var x = 0;
		while (x <= document.cookie.length ) {
			var y = (x+obj.length);
			if(document.cookie.substring(x,y) == obj) {
				if((endOfCookie = document.cookie.indexOf(";",y)) == -1)
					endOfCookie = document.cookie.length;
				return unescape(document.cookie.substring(y,endOfCookie));
			}
			x = document.cookie.indexOf(" ",x)+1;
			if(x == 0) break;
		}
	return "";
}
    
function closeMainPop(key) {
		if($("#todayView").prop("checked")) {
			setCookie('mainPop'+key, 'Y', 1);
		}
		$("#mainPop"+key+"").hide();
}
</script>
profile
개발잘하고시풔!!!!!!!

0개의 댓글