hi can someone please help, im trying to find a way of disabling a link after its been clicked once per user session, can i do this in jquery?
Im brand new to javasript and jquery so could someone please show me what id need to do thanks.
hi can someone please help, im trying to find a way of disabling a link after its been clicked once per user session, can i do this in jquery?
Im brand new to javasript and jquery so could someone please show me what id need to do thanks.
Bind event.preventDefault() to prevent the default browser event of following a link. Do it on subsequent click only (so that on first click it will allow it - this is what you want).
$(".oncelink").one('click',function(e){
$(this).on('click',function(ev){
ev.preventDefault();
});
});
You have to put some check in server side as well for this kind of logic to work. But in client side using jQuery you can do something like
$('a').on('click', function()
{
var me = $(this);
//You can also set some attribute value if you do not want to use class
if(me.hasClass('disabled'))
{
return false;
}
else
{
me.addClass('disabled');
}
});
A way to persist state between HTTP connections is by using cookies. You should really be using a server-side language for cookie setting/getting, as client-side cookie setting/getting can get messy , not to mention be insecure and easy to exploit. Also, if the user has JS turned off in their browser, this wouldn't even work, making your efforts worthless. However, it would look something like this:
// on dom ready
var onceButton = document.getElementById('once');
function disableButton () {
onceButton.disabled = 'disabled';
document.cookie="disable_button=1";
// prevent listening on future clicks
onceButton.removeEventListener('click', disableButton, false);
}
if (document.cookie.indexOf('disable_button=1;') === -1) {
// if no cookie set, wait until click happens to disable button
onceButton.addEventListener('click', disableButton, false);
} else {
// cookie set, disable immediately
disableButton();
}