Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Can't listen to events on window #57

Closed
lazd opened this issue Nov 5, 2014 · 5 comments
Closed

Can't listen to events on window #57

lazd opened this issue Nov 5, 2014 · 5 comments

Comments

@lazd
Copy link
Contributor

lazd commented Nov 5, 2014

I would expect the following code to work (see the Fiddle)

var events = new domDelegate.Delegate(window);

events.on('click', function() {
  console.log('Got a click!');
});

events.on('customEvent', function() {
  console.log('Got a custom event!');
});

window.dispatchEvent(new CustomEvent('customEvent'));

None of the event handlers are called.

I'm using the prebuilt version from http://wzrd.in/standalone/dom-delegate@latest in Chrome 38.0.2125.104 on Mac OS X 10.9.5.

@lazd
Copy link
Contributor Author

lazd commented Nov 5, 2014

I should add that it works fine on document.body, just not on window:

var events = new domDelegate.Delegate(document.body);

events.on('click', function() {
  console.log('Got a click!');
});

events.on('customEvent', function() {
  console.log('Got a custom event!');
});

document.body.dispatchEvent(new CustomEvent('customEvent'));

@matthew-andrews
Copy link
Contributor

If I remember correctly odd, inconsistent (from browser to browser) things happen when you listen to events on the window… @wheresrhys do you remember what we decided to do about this?

@lazd
Copy link
Contributor Author

lazd commented Nov 5, 2014

Things are working predictably using native window.addEventListener (of course), but I also tried Gator and found it worked predictably as well (Fiddle).

@matthew-andrews
Copy link
Contributor

@lazd
Copy link
Contributor Author

lazd commented Nov 5, 2014

Seems to work for bubbled events, but not for events dispatched directly on the window.

With the following setup:

var events = new domDelegate.Delegate(window);

events.on('customEvent', function(evt) {
  console.log('Got a custom event!');
});

This isn't doing anything:

window.dispatchEvent(new CustomEvent('customEvent'));

But this is:

document.body.dispatchEvent(new CustomEvent('customEvent'));

@notlee notlee closed this as completed in 585cb91 Sep 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants