2

Currently I have this to create a Custom event in the JS code in my HTML document.

if (window.CustomEvent) {
  var event = new CustomEvent('not-loaded', {detail: {some: 'data'}});
} else {
  var event = document.createEvent('CustomEvent');
  event.initCustomEvent('not-loaded', true, true, {some: 'data'});
}
this.el.dispatchEvent(event); 

So how would I trigger a function in one of my HTML documents based on when this trigger is activated?

4
  • 3
    Just like any other event handler? someElement.addEventListener('not-loaded', function() { ... }); Commented Aug 12, 2014 at 17:41
  • @FelixKling Which element would I use because I only have div elements in the HTML (long explanation)? Commented Aug 12, 2014 at 17:50
  • 2
    Again, just like with built-in events, you'd bind the handler to the element you want to listen on for the event, or to one of its ancestors. E.g. if I want to perform something when <button> is clicked, I bind the click handler to that button. If you are justing using custom events to pass messages and you actually don't need the DOM at all, something like github.com/mroderick/PubSubJS might be better suited. Commented Aug 12, 2014 at 17:51
  • 1
    Given your example in the question you'd bind it to either this.el or any of its ancestors. Commented Aug 12, 2014 at 17:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.