What is the jQuery Event.namespace

1

In jQuery documentation , we have the explanation of event.namespace .

This allows you to use a jQuery specific event (or create a specific event) with a namespace, through a . point and a name in front of that event.

Example:

$(document).on('click.first_event', function (event)
{
     alert(event.namespace);
});

I would like to know what is the advantage of using events with these namespaces in jQuery?

What is the purpose of these namespaces ?

    
asked by anonymous 23.09.2015 / 14:15

1 answer

0

A event.namespace can be useful for tracking which specific events I want to remove.

Example:

Suppose I have three events of scroll linked to window .

I want to remove one of them specifically, but not all of them.

If I did so:

$(window).scroll(function (){  
      alert('1');
   }

   $(window).scroll(function (){  
      alert('1');
   }

   $(window).off('scroll');

I would remove all events.

But if I did, I would succeed, thanks to event.namespace .

$(window).bind('scroll.alert_1', function ()
{
     alert('1');
});

$(window).bind('scroll.alert_1', function ()
{
     alert('2');
});

$(window).unbind('scroll.alert_1');

In the above case, only alert('2') will be displayed, since I was able to remove a specific event on account of this namespace .

    
23.09.2015 / 14:26