Archive for October, 2004

10.04.04

Custom Javascript Events

Posted in Uncategorized at 10:37 am by UnwashedMeme

One has the capacity to create and fire custom events in Javascript, but I was
unable to find a quick introduction, so here is my example.

function fireEvent(name, target) {
	//Ready: create a generic event
	var evt = document.createEvent("Events")
	//Aim: initialize it to be the event we want
	evt.initEvent(name, true, true); //true for can bubble, true for cancelable
	//FIRE!
	target.dispatchEvent(evt);
}

function foobar() {
	alert("foobar");
}

function testEvents() {
	window.addEventListener("foobar", foobar, false); //false to get it in bubble not capture.
	fireEvent("foobar", document);
}
		

Some references: