简体   繁体   English

编写cookie后触发jquery而不重新加载页面?

[英]Trigger jquery after writing cookie without reloading the page?

How would i go with triggering an alarm or parse it in the html file after writing a js cookie without reloading the page first? 在编写js cookie后不首先重新加载页面的情况下,如何触发警报或将其解析为html文件?

I've tried $(window).bind("load", function () but with no success, the alarm still doesn't trigger without reloading the page, but the cookie is written 我已经尝试过$(window).bind("load", function ()但没有成功,如果不重新加载页面,警报仍然不会触发,但是会写入cookie

My JS 我的JS

$(".addCookie").on("click", function() {
   Cookies.set("cookie", "added"); //adds cookie
})

$('.cookie').html(Cookies.get("cookie")); //writes the cookie in the html file

Codepen 码笔

It looks like a simple case of not understanding asynchronous programming. 看起来像是一个不了解异步编程的简单情况。

This adds a cookie, but not right now. 这会添加一个cookie,但不是现在。 The cookie code will only be evaluated when you click on the .addCookie element on the page: 仅当您单击页面上的.addCookie元素时,才会评估cookie代码:

$(".addCookie").on("click", function() {
   Cookies.set("cookie", "added"); //adds cookie
})

This dumps the cookie, right now , without waiting for you to click: 这转储饼干, 现在 ,无需等待您点击:

$('.cookie').html(Cookies.get("cookie")); //writes the cookie in the html file

If you write the cookie's contents in another onclick handler for another button, you could click that new button to dump the cookie after clicking the first button to set its value. 如果您在另一个按钮的另一个onclick处理程序中写入cookie的内容,则可以单击第一个按钮以设置其值之后单击该新按钮以转储cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM