简体   繁体   English

有没有一种方法可以禁用单击n href链接n天,使用JavaScript中的cookie

[英]Is there a way to disable href link on click for n days use cookies in JavaScript

Is there a way to use cookies to disable a link after click for 5 days and be active again until click then disable for 5 days ...etc 有没有一种方法可以使用Cookie在单击5天后禁用链接,然后再次激活,直到单击然后禁用5天... etc

I have use this for disabling but refreshing page link is active again. 我已使用它来禁用,但是刷新页面链接再次处于活动状态。 I was just thinking using $_user cookie I might be able doing this my problem is getting things together. 我当时只是在考虑使用$ _user cookie来解决这个问题。 Google a lot on how to use cookies but can't seem to get the logic of using it to get my results Google关于如何使用Cookie的很多知识,但似乎无法理解使用它来获取结果的逻辑

<a href="mylink.html" class="disabled">Download</a>
<a href="anotherlink.html" class="disabled">Delete</a>

<script>
    jQuery(document).ready(function($) {
        $('a.disabled').on('click', function(e) {
            e.preventDefault();
        });
    });
</script>

Follow these step to check to obtain the result you desire:- 请按照以下步骤检查以获得所需的结果:-

  1. Now a create function as follow to take in account of the cookie:- 现在创建一个如下考虑cookie的函数:-

    function onclickLink(event, this1){ if($.cookie('link_disabled') === null || $.cookie('link_disabled')==undefined) { /* do whatever you want with link*/ $.cookie("link_disabled", 1, { expires : 5 });/*setting the cookie for 5 days*/ } else{event.preventDefault();} }

  2. Modifiying current event accordingly:- 相应地修改当前事件:

    jQuery(document).ready(function() { $(parent).on('click', 'a.disabled',function(e) { onclickLink(e,$(this)); }); });

Demo 演示版

This fiddle will not work chrome as chrome does not allow to set cookie locally. 由于chrome不允许在本地设置cookie,因此此小提琴无法使用chrome。 So here is the version of the code that is running on the server: link it works in all browsers. 因此,这是服务器上运行的代码的版本: 链接可在所有浏览器中使用。 Comment if any one faces any bug. 评论任何人是否遇到任何错误。

The plugin used is jquery.cookie.js 使用的插件是jquery.cookie.js

Follow these answers to know more about cookies and other stuff: 请按照以下答案来了解有关Cookie和其他内容的更多信息:

  1. https://stackoverflow.com/a/8537083/3190165 https://stackoverflow.com/a/8537083/3190165

  2. https://stackoverflow.com/a/2824037/3190165 https://stackoverflow.com/a/2824037/3190165

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

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