PHP Classes

File: eventos.js

Recommend this page to a friend!
  Classes of José M. Carnero   Formulario   eventos.js   Download  
File: eventos.js
Role: Auxiliary data
Content type: text/plain
Description: Validations, javascript, events
Class: Formulario
Generate and validate HTML forms
Author: By
Last change:
Date: 14 years ago
Size: 3,568 bytes
 

Contents

Class file image Download
/* guarda el ultimo evento que se ha disparado, sirve como controlador para que otros eventos puedan lanzarse (o no) en funcion del previo asignar en cada funcion afectada (las que se lancen en los eventos), donde interese */ var nombreEvento = ''; /** * Evita la propagacion de eventos. * Recordar llamar desde cada funcion que lo necesite. * * @param e Objeto evento * @return boolean * @access public */ function noPropagarEvento(e){ if(e.preventDefault){ //e.stopPropagation modelo DOM e.stopPropagation(); e.preventDefault(); } else if(window.event){ //window.event.cancelBubble modelo MSIE //window.event.keyCode = 0; //<<< esto ayuda a que funcione bien en iExplorer window.event.cancelBubble = true; window.event.returnValue = false; window.event.retainFocus = true; } //alert(e); return true; } /** * Manejador de eventos. * * @param elemento Nombre del elemto (DOM); cadena o el propio objeto * @param evento Nombre del evento, ej: "click" (como "onclick" sin "on") * @param funcion Funcion que se lanzara con el evento; cadena de nombre de funcion o nombre de la funcion sin mas, tambien se permiten funciones anonimas: "function(){ alert('hello!'); }" * @return boolean * @access public */ function addEvent(elemento, evento, funcion){ var tRet; //TODO concatena a la definicion de la funcion la asignacion de "nombreEvento" para controlar que evento ha sido disparado /*funcion = eval(funcion.toString() + " window.nombreEvento='" + evento + "';"); alert(funcion.toString());*/ if(typeof elemento=="string") elemento = document.getElementById(elemento); if(typeof funcion=="string") funcion = window[funcion]; //if(typeof funcion=="string") funcion = eval('function(e){ ' + funcion + ' }'); if(typeof argumentos == 'undefined') argumentos = ''; if(!elemento || typeof evento!="string") return false; evento = evento.toLowerCase(); if(elemento.addEventListener){ elemento.addEventListener(evento, funcion, false); //ultimo parametro //elemento.addEventListener(evento, eval('function(){ ' + funcion + ' }'), false); tRet = true; } else if(elemento.attachEvent){ elemento['e' + evento + funcion] = funcion; elemento[evento + funcion] = function(){ elemento['e' + evento + funcion]( window.event );} tRet = elemento.attachEvent('on' + evento, elemento[evento + funcion]); //var tRet = elemento.attachEvent('on' + evento, eval('function(){ ' + funcion + ' }')); } else{ eval("elemento.on"+evento+" = funcion;"); tRet = true; } //var temp = function (e){noPropagarEvento(e);} var elementoOrigen = function (e) { if (!e) var e = window.event; var relTarg = e.relatedTarget || e.fromElement; return(relTarg); } //If you want to know where the mouse goes to in case of mouseout, do: var elementoDestino = function (e) { if (!e) var e = window.event; var relTarg = e.relatedTarget || e.toElement; return(relTarg); } return tRet; } function removeEvent(elemento, evento, funcion){ if(typeof elemento=="string") elemento = document.getElementById(elemento); if(typeof funcion=="string") funcion = window[funcion]; //if(typeof funcion=="string") funcion = eval('function(e){ ' + funcion + ' }'); if(typeof argumentos == 'undefined') argumentos = ''; if(!elemento || typeof evento!="string") return false; if(elemento.addEventListener){ elemento.removeEventListener(evento, funcion, false); } else if(elemento.attachEvent){ elemento.detachEvent('on'+evento, elemento[evento+funcion]); elemento[evento+funcion] = null; } return true; }