I'm trying to disable a button if the user is currently on that page, I know there is more efficient ways but i am practicing my if, elses.
The problem is that I am still able to click the links and both will refresh the page if I click them and i'm on the same page. My code:
$("#homepage").on("click", function(event) {
if (window.location.href === 'index.html') {
$(this).attr('disabled', "disabled");
$(this).attr('disabled', true);
event.preventDefault();
}else {
window.location.href = 'index.html';
};
});
$("#aboutUs").on("click", function(event) {
if (window.location.href === 'aboutus.html') {
$(this).attr('disabled', "disabled");
$(this).attr('disabled', true);
event.preventDefault();
}else {
window.location.href = 'aboutus.html';
};
});
Actually
window.location.hrefwill give you full-fledged URL like:-etc.
That's why it seems that your
ifcondition never becomes true.Use
indexOf()like below:-Reference:-
indexOf() functionality
If you want to disable buttons on only two specific URL'S then use full path in your
ifcondition provided byconsole.log(window.location.href);.