我在网站的主页上做了一个弹出窗口,每次登录主页时会弹出这个窗口。但现在想对这个弹出窗口做一个限制。就是在弹出窗口的下面添加一个关闭按钮,当关闭这个按钮的时候,会关闭一天的弹出窗口,第二天登录主页的时候才会弹出这个窗口。php页面代码
<a><img src="/images/pop.jpg"/></a>
<div id="chk" style="position:absolute; top:325px;left:5px">
<input type="checkbox"> <b> if click close button, it close one day</b>
<input type="button" value="close" id="closebutton">
</div>
<script>
$('a').click(function(){
//opener.location = '/page/view/howto';
window.open('/boards/notice/detail/182');
window.close();
});
$("#closebutton").click(function(){
window.close();
var values = 1;
setcookie("name",values,time()+60);
});
</script>
index.js页面代码
(function($){
$(document).ready(function(){
if(这里如何获取php页面中设置的cookie){
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
});
})(jQuery);
我在php页面里做了,当用户关闭这个弹出窗口的时候生成一个cookie。时间设置一天。
在调用弹出窗口的index.js页面里用这个cook名称来判断cookie是否存在,如果存在就不执行弹出窗口的代码。等到第二天,cookie消失了,就执行这段弹出窗口的代码,代码在上面,现在问题是index.js页面里如何获取php页面里设置的cookie?
<a><img src="/images/pop.jpg"/></a>
<div id="chk" style="position:absolute; top:325px;left:5px">
<input type="checkbox"> <b> if click close button, it close one day</b>
<input type="button" value="close" id="closebutton">
</div>
<script>
$('a').click(function(){
//opener.location = '/page/view/howto';
window.open('/boards/notice/detail/182');
window.close();
});
$("#closebutton").click(function(){
window.close();
var values = 1;
setcookie("name",values,time()+60);
});
</script>
index.js页面代码
(function($){
$(document).ready(function(){
if(这里如何获取php页面中设置的cookie){
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
});
})(jQuery);
我在php页面里做了,当用户关闭这个弹出窗口的时候生成一个cookie。时间设置一天。
在调用弹出窗口的index.js页面里用这个cook名称来判断cookie是否存在,如果存在就不执行弹出窗口的代码。等到第二天,cookie消失了,就执行这段弹出窗口的代码,代码在上面,现在问题是index.js页面里如何获取php页面里设置的cookie?
{ var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
//if(empty($_COOKIE["name"])){
if(!getCookie("name")){
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> - </title>
<script type='text/javascript' src="jquery-1.5.2.min.js"></script>
<script type='text/javascript'>
(function($){
//擴展方法,讀寫cookie
//$.setCookie("a1","abc",999);寫
//aaa=$.getCookie("a1");讀
$.extend({getCookie:function(sName){
var aCookie=document.cookie.split("; ");
for(var i=0;i<aCookie.length;i++){
var aCrumb=aCookie[i].split("=");
if(sName==aCrumb[0]){return decodeURIComponent(aCrumb[1]);}
}
return '';
},setCookie:function(sName,sValue,sExpires){
var sCookie=sName+"="+encodeURIComponent(sValue);
if(sExpires!=null){sCookie+="; expires="+sExpires;}
document.cookie=sCookie;
},removeCookie:function(sName){
document.cookie=sName+"=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}});
})(jQuery)$(function(){
//需要給cookie設置值的時候這樣設置
$.setCookie("isopen","abc",(1*24*3600)); //判斷cookie是否為空
_c=$.getCookie("isopen");
if(_c.length){
alert('Cookie的值是:'+_c);
}else{
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
});
</script>
</head><body>
</body>
</html>
<a><img src="/images/pop.jpg"/></a>
<div id="chk" style="position:absolute; top:325px;left:5px">
<input type="checkbox"> <b> if click close button, it close one day</b>
<input type="button" value="close" id="closebutton">
</div>
<script>
$('a').click(function(){
//opener.location = '/page/view/howto';
window.open('/boards/notice/detail/182');
window.close();
});
$("#closebutton").click(function(){
window.close();
$.setCookie("isopen","abc",(1*24*3600));
});
</script>index.js
(function($){
$(document).ready(function(){
//判斷cookie是否為空
_c=$.getCookie("isopen");
if(_c.length){
alert('Cookie的值是:'+_c);
}else{
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
});
})(jQuery);因为setcookie是在php页面写的,而获取cookie(getcookie)要在index.js页面中获取的。所以按照上面那样写可以么? 那下面这些代码还要写么?写的话,写哪里好呢? 是写在index.js代码里么?
$.extend({getCookie:function(sName){
var aCookie=document.cookie.split("; ");
for(var i=0;i<aCookie.length;i++){
var aCrumb=aCookie[i].split("=");
if(sName==aCrumb[0]){return decodeURIComponent(aCrumb[1]);}
}
return '';
},setCookie:function(sName,sValue,sExpires){
var sCookie=sName+"="+encodeURIComponent(sValue);
if(sExpires!=null){sCookie+="; expires="+sExpires;}
document.cookie=sCookie;
},removeCookie:function(sName){
document.cookie=sName+"=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}});
popup.php页面代码
<a><img src="/images/pop.jpg"/></a>
<div id="chk" style="position:absolute; top:325px;left:5px">
<input type="checkbox"> <b> if click close button, it close one day</b>
<input type="button" value="close" id="closebutton">
</div>
<script>
$('a').click(function(){
//opener.location = '/page/view/howto';
window.open('/boards/notice/detail/182');
window.close();
});
$("#closebutton").click(function(){
window.close();
$.setCookie("isopen","abc",(1*24*3600)); });
</script>
index.js页面代码
(function($){
$(document).ready(function(){ $.extend({getCookie:function(sName){
var aCookie=document.cookie.split("; ");
for(var i=0;i<aCookie.length;i++){
var aCrumb=aCookie[i].split("=");
if(sName==aCrumb[0]){return decodeURIComponent(aCrumb[1]);}
}
return '';
},setCookie:function(sName,sValue,sExpires){
var sCookie=sName+"="+encodeURIComponent(sValue);
if(sExpires!=null){sCookie+="; expires="+sExpires;}
document.cookie=sCookie;
},removeCookie:function(sName){
document.cookie=sName+"=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}});
_c=$.getCookie("isopen");
if(_c.length){
alert('Cookie的值是:'+_c);
}else{
var popup = window.open('/main/popup', 'popup', 'top=0,left=0,toolbar=0,directories=0,menubar=0,resizable=0,scrollbars=0,width=300,height=350');
if(popup){popup.focus();}
}
});
})(jQuery);