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

Mathematics

Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on ScienceLibrary.info.

Mathematics

Great Scientists

Carl Linnaeus

1707 - 1778

Carl Linnaeus was a prolific writer, publishing books, lavishly illustrated, throughout his life. Through his travels, studies and collections, he developed a system of taxonomic nomenclature which is the basis of the modern system.

Carolus Linnaeus
Umwelt.Science

Quote of the day...

An unsophisticated forecaster uses statistics as a drunken man uses lamp-posts - for support rather than for illumination.

ZumGuy Internet Promotions

English language school