I am using jQuery 1.10.2, but would also like to know the solution for the most modern version if it is not the same.
I have a JavaScript file that will add and remove some custom events.
$(window).on('clicked_button', function(event) {
// Do stuff
});
if (someCriteria) {
$(window).off('clicked_button');
}
if (otherCriteria) {
$(window).trigger('clicked_button');
}
I know that you can loop through the events in the data object to check if the event is already bound or not, but is that necessary? I don't care if the event is already bound or not, I just want to make sure it won't be bound after someCriteria
is met.
When I run $(window).off('clicked_button');
in the js console on Firefox, it just returns the window object. Can I assume then that the off()
function will not throw an error if the event isn't already bound? I'd prefer to just use off()
and let it silently fail, than having to loop through the bound events before unbinding it.
I would need this to work for the modern browsers and IE 9+.