简体   繁体   中英

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

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. Google a lot on how to use cookies but can't seem to get the logic of using it to get my results

<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:-

    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. 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

Follow these answers to know more about cookies and other stuff:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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