如何在JavaScript中创建一个弹出式Cookie? [英] how to create a popup with cookies in javascript?

查看:83
本文介绍了如何在JavaScript中创建一个弹出式Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个弹出窗口,并在30秒内设置cookie过期,并在页面加载一次时显示一个弹出窗口。问题是我弹出它的弹出窗口,但我的饼干不起作用我不知道我的代码有什么问题我累了几件事,但它没有工作,我也看了几个videos.I今天早上,如果你可以工作,帮助我与你一起做我的一天。任何帮助提前感谢。

 <!DOCTYPE html>< html lang =en> ;< HEAD> <! -  Bootstrap Core CSS  - > < link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css> <! - 最新的jQuery库 - > < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script> <! -  Bootstrap Core JS  - > < script src =https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js>< / script> <! - 用于Modal的Cookie JS  - > < script src =http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js>< / script> < script type =text / javascript> $(文件)。就绪(函数(){$( #myModal)模式(秀);}); < / script>< / head>< body>< div class =container-fluid>< div id =myModalclass =modal fade> < div class =modal-dialog> < div class =modal-content> < div class =modal-header> < button type =buttonclass =closedata-dismiss =modalaria-hidden =true>& times;< / button> < h3>用于秋季会话的注册< / h3> < / DIV> < div class =modal-body> < div class =row> < div class =col-xs-12 col-sm-6> < img src =gym.pngalt =gym_promostyle =width:304px;> < / DIV> < div class =col-xs-12 col-sm-6> < H3>立即保留您的拍品< / h3> < p为H. EMAIL:< a href =mailto:PLAYATGYM@GMAIL.COMtarget =_ top> PLAYATGYM@GMAIL.COM< / a> < / p为H. < p> OR CALL:< a href =514-795-4266> 514-795-4266< / A> < / p为H. <脚本> createCookie的();< /脚本> < / DIV> < / DIV> < / DIV> < / DIV> < / DIV>< / DIV>< / DIV> <脚本>函数createCookie(name,value){var date = new Date(); date.setTime(date.getTime()+(30 * 1000)); var expires =; expires =+ date.toGMTString(); document.cookie = name +=+ value + expires +; path = /;}< / script>< / body>< / html>  

当您致电createCookie()你不传递参数,所以名字和值都是未定义的,不正确地设置cookie。



另外,您添加的用于创建cookie的脚本将在页面加载时被调用。即使模式没有显示,它仍然被加载,所以createCookie()函数被调用。如果你想在弹出的弹出窗口中调用它,请将该行添加到上面的jquery中:

  $(document ).ready(function(){
$(#myModal)。modal('show');
createCookie(cookie的名称,cookie的值);
});


I'm trying to create a pop up and set cookies expire in 30 sec and displays a popup when the page loads just once . the problem is my popup it pop ups but my cookies does not work I do not know whats wrong with my code i tired few things but it did not work also i watched few videos.I'm working on this since this morning if you could help me with you are making my day.any help appreciate it in advance.

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Bootstrap Core CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <!-- Latest jQuery Library -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <!-- Bootstrap Core JS -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  <!-- Cookie JS for Modal -->
  <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js"></script>
  <script type="text/javascript">
	$(document).ready(function(){
		$("#myModal").modal('show');
	});
	
	
</script>
</head>

<body>

<div class="container-fluid">
<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h3>REGISTER FOR FALL SESSION</h3>
			    
            </div>
			
            <div class="modal-body">
			<div class="row">
				<div class="col-xs-12 col-sm-6">
                <img src="gym.png" alt="gym_promo" style="width:304px;">
				</div>
				
			<div class="col-xs-12 col-sm-6">
				<h3> Reserve Your Spot Today </h3>
				<p> EMAIL : <a href="mailto:PLAYATGYM@GMAIL.COM" target="_top">PLAYATGYM@GMAIL.COM </a> </p>
				
				<p>OR CALL :<a href="514-795-4266"> 514-795-4266</a> </p>
				
				
				<script>createCookie();</script>
						  
            </div>
			</div>
			</div>
        </div>
    </div>
</div>
</div>

  <script>
   function createCookie(name, value) {
   var date = new Date();
   date.setTime(date.getTime()+(30*1000));
   var expires = "; expires="+date.toGMTString();

   document.cookie = name+"="+value+expires+"; path=/";
}
  </script>

</body>
</html>                            

解决方案

When you call "createCookie()" you don't pass parameters, and so name and value are undefined, setting the cookie incorrectly.

Also, the script you added to create the cookie will be called when the page loads. Even if the modal isn't shown, it is still loaded, and so the createCookie() function is called. If you want it to be called when the popup pops up, add the line to the jquery you have above:

$(document).ready(function(){
    $("#myModal").modal('show');
    createCookie("name of cookie","value of cookie");
});

这篇关于如何在JavaScript中创建一个弹出式Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆