0

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>
user2686655
  • 71
  • 1
  • 1
  • 10

1 Answers1

0

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

Community
  • 1
  • 1
Gaurav Kalyan
  • 1,897
  • 2
  • 14
  • 21
  • I use this [plugin](https://github.com/carhartl/jquery-cookie) for cookie, I have written the code according to it. you can use any other plugin also or if you want you can hard code it. – Gaurav Kalyan Nov 30 '14 at 13:28
  • Hi, I learn by by practical experience like jsfiddle to understand the behavior of script I made a small index file referencing to:- but don't know where to place the function code herewith an example of my index.php file. – user2686655 Dec 03 '14 at 14:13
  • Sorry, replace above comment: Hi, I learn by by practical experience like jsfiddle to understand the behavior of script I have added in heading but don't know where to place the function and how – user2686655 Dec 03 '14 at 14:21
  • If it helps, I will create jsFiddle for the answer that I have written. But it would great if you share the jsfiddle you have created so that the problem can be analyzed which will help you in the future. – Gaurav Kalyan Dec 03 '14 at 15:42
  • I haven't created a jsfiddel Just a small php file with your code and placed jquery.cookie.js in root directory as my file is 2 sub directories down and to see if I can understand & get code working. Just don't know how to add the function code to see if it is working In in Download – user2686655 Dec 03 '14 at 18:50
  • In in Download – user2686655 Dec 03 '14 at 18:55
  • I have created a [fiddle](http://jsfiddle.net/73vzD/191/) for it. Look into it. If still you have some issues, ask me. – Gaurav Kalyan Dec 03 '14 at 19:05
  • Just copy paste the code anywhere below calling the script jquery.cookie.js. The order of calling the script should be 1. jquery script 2. jquery.cookie.js 3. the above code written. – Gaurav Kalyan Dec 03 '14 at 19:13
  • It will be highly appreciated if you could create js fiddel so to help me understand the scenario – user2686655 Dec 04 '14 at 03:48
  • I have updated the code so that it will run in all the browsers look into it [Demo](http://jsfiddle.net/73vzD/204/) – Gaurav Kalyan Dec 04 '14 at 15:18
  • Thaxs, work 100% I have now a more understanding of jquery cookie – user2686655 Dec 04 '14 at 19:59