JavaScript Dispatching CustomEvent with dot in name do not trigger jQuery.on() event listener dispatch custom event with data

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue?

This code works:

window.addEventListener('test.namespace', e => console.log('CustomEvent with namespace captured by vanilla'));

This does not work:

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

const event = new CustomEvent('test.namespace', {
  bubbles: true,
  detail: 123
});
window.dispatchEvent(event);

http://jsbin.com/tecupavuji/edit?html,js,console

Answer:1

The "normal" DOM event system doesn't have a concept of namespaces. window.addEventListener('test.namespace', ...) will literally be listening for an event with the name test.namespace, which is what you are creating with new CustomEvent('test.namespace', ...).

Event namespaces is a concept in jQuery:

An event name can be qualified by event namespaces that simplify removing or triggering the event. For example, "click.myPlugin.simple" defines both the myPlugin and simple namespaces for this particular click event. A click event handler attached via that string could be removed with .off("click.myPlugin") or .off("click.simple") without disturbing other click handlers attached to the elements.

What's important here is that the namespace is not part of the event name. That's why new CustomEvent('test.namespace', ...) doesn't work, but new CustomEvent('test', ...) would.

If you want to trigger an event for a specific namespace, you have to use jQuery for that:

$(window).trigger('test.namespace', {detail: 123});

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

$(window).trigger('test.namespace', {detail: 123});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Answer:2

I have a 3 block of image. I want a css in which hover a block A transform block C and B, hovering a block B transform block A and C or hovering a block C transform block A and B You can see a demo ...

I have a 3 block of image. I want a css in which hover a block A transform block C and B, hovering a block B transform block A and C or hovering a block C transform block A and B You can see a demo ...

var developers = [ { name: "Joe", age: 23 ,overallLevel: "high"}, { name: "Sue", age: 28 ,overallLevel: "advanced" }, { name: "Jon", age: 32 ,overallLevel: "high" }, { name: "Bob", age:...

var developers = [ { name: "Joe", age: 23 ,overallLevel: "high"}, { name: "Sue", age: 28 ,overallLevel: "advanced" }, { name: "Jon", age: 32 ,overallLevel: "high" }, { name: "Bob", age:...

  1. javascript code count
  2. countdown javascript code
  3. javascript code to count the number of elements in a form
  4. javascript code to count number of visitors
  5. javascript code to count characters in textbox
  6. javascript code to count number of words
  7. javascript code for word count
  8. javascript code for character count
  9. write javascript code to count sum of digits

I have a time/date string that is returned like so: "5/31/2016, 2:23:33 PM". I need only the HH:MM and am/pm, so 2:23 PM I am able to split the date off but not sure how I can remove the colon and ...

I have a time/date string that is returned like so: "5/31/2016, 2:23:33 PM". I need only the HH:MM and am/pm, so 2:23 PM I am able to split the date off but not sure how I can remove the colon and ...

  1. javascript slice time complexity
  2. slice time javascript

I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For ...

I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For ...