Science Library - free educational site

Event handlers

Personalised Events in JavaScript

Often it is useful to augment the available events by creating personalised events. The simplest method is as follows:

var event = new CustomEvent('eventName');

//Listen to an element:

elem.addEventListener('eventName', function(e){...});

// Trigger

event.dispatchEvent(event);

This method functions on all up-to-date browsers - except Internet Explorer. To make it functionable with antiquated browsers and IE, we can use an alternative method:

function createEvent(eventType) {

try {

return new CustomEvent(eventType);

} catch (error) { // CustomEvent not supported

if (document.createEvent && document.createEvent.initEvent) {

// old browsers

var ev = document.createEvent("HTMLEvents");

ev.initEvent(eventType, true, true);

} else if (document.createEventObject) {

// IE

var ev = document.createEventObject();

ev.eventType = eventType;

} else {

return false;

}

return ev.

}

}

At this point, however, it is necessary to bear in mind that the events created with createEventObject do not have the dispatchEvent method, but use instead fireEvent.

function triggerEvent(element, event) {

if (element.dispatchEvent) {{

element.dispatchEvent(event);

} else if (element.fireEvent) {

element.fireEvent("on" + event.eventType, event);

} else {

return false;

}

return true

}

In this way we can create and use our own personalised events!

Sean Bone

ZumGuy Network webmaster

Visit my Internet forum: ZumGuy Internet Forum

Per una versione di quest'articolo in Italiano vedi: Eventi personalizzati in JavaScript

Personalised Events in JavaScript

Often it is useful to augment the available events by creating personalised events. The simplest method is as follows:

var event = new CustomEvent('eventName');

//Listen to an element:

elem.addEventListener('eventName', function(e){...});

// Trigger

event.dispatchEvent(event);

This method functions on all up-to-date browsers - except Internet Explorer. To make it functionable with antiquated browsers and IE, we can use an alternative method:

function createEvent(eventType) {

try {

return new CustomEvent(eventType);

} catch (error) { // CustomEvent not supported

if (document.createEvent && document.createEvent.initEvent) {

// old browsers

var ev = document.createEvent("HTMLEvents");

ev.initEvent(eventType, true, true);

} else if (document.createEventObject) {

// IE

var ev = document.createEventObject();

ev.eventType = eventType;

} else {

return false;

}

return ev.

}

}

At this point, however, it is necessary to bear in mind that the events created with createEventObject do not have the dispatchEvent method, but use instead fireEvent.

function triggerEvent(element, event) {

if (element.dispatchEvent) {{

element.dispatchEvent(event);

} else if (element.fireEvent) {

element.fireEvent("on" + event.eventType, event);

} else {

return false;

}

return true

}

In this way we can create and use our own personalised events!

Sean Bone

ZumGuy Network webmaster

Visit my Internet forum: ZumGuy Internet Forum

Per una versione di quest'articolo in Italiano vedi: Eventi personalizzati in JavaScript

Content © Renewable-Media.com. All rights reserved. Created : June 3, 2014 Last updated :March 7, 2016

Latest Item on Science Library:

The most recent article is:

Trigonometry

View this item in the topic:

Vectors and Trigonometry

and many more articles in the subject:

Subject of the Week

Universe

'Universe' on ScienceLibrary.info covers astronomy, cosmology, and space exploration. Learn Science with ScienceLibrary.info.

Science

Great Scientists

Daniel Bernoulli

1700 - 1782

Daniel Bernoulli was a major scientist and mathematician of the 18th century, a friend and associate of Leonard Euler, and a member of the illustrious Bernoulli dynasty of mathematicians.

Daniel Bernoulli, 1700 - 1782
IT Forum by Sean Bone

Quote of the day...

I saw in a dream a table where all elements fell into place as required. Awakening, I immediately wrote it down on a piece of paper, only in one place did a correction later seem necessary.

ZumGuy Internet Promotions

Vitruvian Boy