Created
October 7, 2012 10:02
-
-
Save demonixis/3847735 to your computer and use it in GitHub Desktop.
Utilisation d'events perso en JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fonction pour déclencher un évènement sur un élément du dom | |
function triggerEvent(element, eventName) | |
{ | |
if ((element[eventName] || false) && typeof element[eventName] == 'function') | |
{ | |
element[eventName](element); | |
} | |
} | |
// Récupération d'un élément du dom | |
var tonElemDom = document.getElementById("monElem"); | |
// Lancer un event click sur ton élément du dom | |
JSHelper.triggerEvent(tonElemDom, "click"); | |
// -------------------------- | |
// 2 - Créer ses events perso | |
// -------------------------- | |
// 1 - Création de l'event | |
var monEvent = document.createEvent("HTMLEvents"); | |
monEvent.initEvent("spritePositionChanged", true, false); // Le 2éme paramètre est le nom de ton event perso | |
// 2 - Lancer l'event | |
monEvent.spritePosition = sprite.position; // On passe autant de valeur que l'on veux à l'event | |
document.dispatchEvent(monEvent); // On lance l’évènement (Quand c'est nécessaire bien entendu) | |
// 3 - Utiliser l'event | |
document.addEventListener("spritePositionChanged", function (e) | |
{ | |
var newPos = e.position; | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment