我正在努力在HTML页面上使用Cookie。我想要一个脚本检查以查看用户是否有cookie,如果没有调用此脚本打开模型窗口
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myModal").reveal();
});
</script
该窗口包含“是”和“否”按钮,如果您选择“是”,它将保存一个cookie,以便在将来访问该页面时不会打开该模式,如果用户选择“否”,它将会只需将它们引导到另一页。
答案 0 :(得分:1)
1)使用jQuery cookie库以方便使用。 https://github.com/carhartl/jquery-cookie
2)检查cookie是否存在并进行相应处理
if ($j.cookie("NameOfYourCookie") == null) {
//Do stuff if cookie doesn't exist like set a cookie with a value of 1
$j.cookie("NameOfYourCookie", 1, {expires: 10, path:'/'});
}
else {
//They have a cookie so do something
alert ('You have a cookie with name NameOfYourCookie');
}
*编辑*
以为我可能会编辑我的答案,以便更适合您的问题。
<button class="yes">YES</button>
<button class="no">No</button>
var $j = jQuery.noConflict();
$j(document).ready(function(){
//Show modal on page load
$j("#myModal").show();
$j('.yes').click(function(){
$j("#myModal").hide();
$j.cookie("YesButtonCookie", 1, {expires: 10, path:'/'});
alert ('You have clicked yes, a cookie has been dropped');
});
if ($j.cookie("YesButtonCookie") == 1){
// Don't show the div
$j("#myModal").hide();
alert ('Cookie found, you cant see myModal');
}
if ($j.cookie("YesButtonCookie") == null){
// Cookie Not Found
alert ('Cookie Not found, you can see myModal');
$j("#myModal").show();
}
$j('.clearcookie').click(function(){
$j.cookie("YesButtonCookie", null);
alert('Cookie Cleared');
});
});