Foros del Web » Programando para Internet » Javascript »

Problema con objeto en IE

Estas en el tema de Problema con objeto en IE en el foro de Javascript en Foros del Web. Sigo teniendo problemas con IE . Tengo esta función, pero me dice que esperaba un objeto (en FF y Chrome sí funciona) : @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); ...
  #1 (permalink)  
Antiguo 27/01/2011, 14:28
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Problema con objeto en IE

Sigo teniendo problemas con IE . Tengo esta función, pero me dice que esperaba un objeto (en FF y Chrome sí funciona) :
Código Javascript:
Ver original
  1. // [...]
  2.   opacity : function(){
  3.    var isIE = document.all, estilos = isIE ? ['filter', 100] : ['opacity', 1], valor = isIE ? ['alpha(opacity=', ')'] : ['', ''];
  4.    alert(this.tagName); // undefined según IE ¬¬
  5.    if(arguments.length){
  6.     this.style[estilos[0]] = valor[0] + (arguments[0] * estilos[1]) + valor[1];
  7.    }else{
  8.     if(this.style[estilos[0]]){
  9.      return this.style[estilos[0]].match(/\d+(\.\d+)?/)[0]/estilos[1];
  10.     }else{
  11.      if(SS.find(this.tagName, isIE?'filter':'opacity')){
  12.       return isIE?SS.find(this.tagName, 'filter').match(/\d+(\.\d+)?/)[0]/100:SS.find(this.tagName, 'opacity');
  13.      }else{
  14.       if(SS.find('#'+this.id, isIE?'filter':'opacity'))
  15.        return isIE?SS.find('#'+this.id, 'filter').match(/\d+(\.\d+)?/)[0]/100:SS.find('#'+this.id, 'opacity');
  16.       else{
  17.        for(var i=0, partir=this.className.split(' '); clase=partir[i]; i++){
  18.         if(SS.find('.'+clase, isIE?'filter':'opacity'))
  19.          return isIE?SS.find('.'+clase, 'filter').match(/\d+(\.\d+)?/)[0]/100:SS.find('.'+clase, 'opacity');
  20.        }
  21.       }
  22.      }
  23.     }
  24.    }
  25.    return 1;
  26.   }
  27. // [...]
¿Alguien me puede ayudar?
Muchas gracias :D
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #2 (permalink)  
Antiguo 29/01/2011, 07:51
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

Que tal _cronos2, opacity es un método de que objeto ?
__________________
http://es.phptherightway.com/
thats us riders :)
  #3 (permalink)  
Antiguo 29/01/2011, 08:00
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

Es de una librería que estoy intentando montarme XD El código es muy largo pero dejo lo importante:
Código Javascript:
Ver original
  1. var JSPlus = (function(){
  2.  var privado = {
  3.   // [...]
  4.   opacity : function(){
  5.    var isIE = document.all, estilos = isIE ? ['filter', 100] : ['opacity', 1], valor = isIE ? ['alpha(opacity=', ')'] : ['', ''];
  6.    if(arguments.length){
  7.     this.style[estilos[0]] = valor[0] + (arguments[0] * estilos[1]) + valor[1];
  8.    }else{
  9.     if(this.style[estilos[0]]){
  10.      return this.style[estilos[0]].match(/\d+(\.\d+)?/)[0]/estilos[1];
  11.     }else{
  12.      if(SS.find(this.tagName, estilos[0])){
  13.       return SS.find(this.tagName, estilos[0]).match(/\d+(\.\d+)?/)[0]/estilos[1];
  14.      }else{
  15.       if(SS.find('#'+this.id, estilos[0]))
  16.        return SS.find('#'+this.id, estilos[0]).match(/\d+(\.\d+)?/)[0]/estilos[1];
  17.       else{
  18.        for(var i=0, partir=this.className.split(' '); clase=partir[i]; i++){
  19.         if(SS.find('.'+clase, estilos[0]))
  20.          return SS.find('.'+clase, estilos[0]).match(/\d+(\.\d+)?/)[0]/estilos[1];
  21.        }
  22.       }
  23.      }
  24.     }
  25.     return 1;
  26.    }
  27.    return P(this);
  28.   },
  29.   // [...]
  30.  };
  31.  return {
  32.   extender : function(elem, obj){
  33.    if(elem.extendido && elem != privado) return elem;
  34.    for(var i in obj){
  35.     elem[i] = obj[i];
  36.    }
  37.    return elem;
  38.   },
  39.   get : function(obj){
  40.    switch(typeof obj){
  41.     case 'object':
  42.      return JSPlus.extender(obj, privado);
  43.     break;
  44.     case 'string':
  45.      return JSPlus.extender(document.querySelector(obj), privado);
  46.     break;
  47.    }
  48.    return null;
  49.   }
  50.  };
  51. })();
  52. var P = JSPlus.get;
Entonces si por ejemplo tengo un código así:
Código Javascript:
Ver original
  1. P('#img').opacity(0.3);
Funciona en FF y Chrome, pero en IE me dice que se esperaba un objeto (en la línea de this.style[estilos[0]] = valor[0] + (arguments[0] * estilos[1]) + valor[1]; )
Saludos (:
PD: Gracias a @Panino por la idea del mixin, jamás se me hubiera ocurrido XD
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #4 (permalink)  
Antiguo 29/01/2011, 08:24
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

En que versión de IE ?, en la 8 va bien.
__________________
http://es.phptherightway.com/
thats us riders :)
  #5 (permalink)  
Antiguo 29/01/2011, 08:37
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

Cierto, acabo de probar el código y sí funciona (IE9). Entonces es un problema de scope, porque en realidad el código con el que estaba haciendo pruebas era este:
Código Javascript:
Ver original
  1. P('#img').hover(function(){ // onmouseover
  2.  P(this).entrada(1000); // fadeIn
  3. });
pero como el error me lo marcaba en la función opacity (porque para entrada uso opacity) no se me ocurrió pensar en el scope Pero sigue siendo problema de IE, porque en los demás me funciona. ¿Se te ocurre algo?
Muchas gracias por la ayuda :D
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #6 (permalink)  
Antiguo 29/01/2011, 08:53
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

Habría que ver el código de hover y entrada, lo podes postear ?
__________________
http://es.phptherightway.com/
thats us riders :)
  #7 (permalink)  
Antiguo 29/01/2011, 09:00
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

Aquí está:
Código Javascript:
Ver original
  1. hover : function(toDo){
  2.    addEvent('mouseover', toDo, this);
  3.    return P(this);
  4.   },
  5.  entrada : function(t){
  6.    var tempo = { 'lento' : 800, 'medio' : 500, 'rapido' : 200};
  7.    var tiempo = isFinite(t)?t:tempo[t], thix = P(this), incr = 20/tiempo;
  8.    thix.opacity(0);
  9.    var timer = setInterval(function(){
  10.     if(thix.opacity()>=1) timer = clearInterval(timer);
  11.     else{
  12.      thix.opacity(thix.opacity()+incr);
  13.     }
  14.    }, 20);
  15.    return P(this);
  16.   }
Y la función addEvent:
Código Javascript:
Ver original
  1. function addEvent(tipo, toDo, obj){
  2.  if(obj.attachEvent)
  3.   obj.attachEvent('on'+tipo, toDo, false);
  4.  else{
  5.   if(obj.addEventListener)
  6.    obj.addEventListener(tipo, toDo, false);
  7.   else
  8.    obj['on'+tipo]=toDo;
  9.  }
  10.  return obj;
  11. }
Saludos (:
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #8 (permalink)  
Antiguo 29/01/2011, 09:59
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

En addEvent esta el culpable, attachEvent no funciona como addEventListener, no te bindea el scope, dentro de la funcion this hace referencia a window, una forma rapida de solucionarlo seria algo asi:

Código Javascript:
Ver original
  1. ...
  2. if(obj.attachEvent){                                   
  3.    obj.attachEvent('on'+tipo, function() { toDo.call(obj, window.event); }, false);                
  4. }
  5. ...

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)

Última edición por masterpuppet; 29/01/2011 a las 10:06 Razón: corrección typo
  #9 (permalink)  
Antiguo 29/01/2011, 10:15
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

¡Sííííííííííííííííííííííííííííííííííí! Borré esa parte del código de @Panino porque no entendí bien lo que hacía y pensé que no servía para nada Aquí dejo cómo quedó el código:
Código Javascript:
Ver original
  1. function addEvent(tipo, toDo, obj){
  2.  if(obj.attachEvent){
  3.   var thix = obj, fn = function(){ toDo.call(thix, window.event); };
  4.   obj.attachEvent('on'+tipo, fn, false);
  5.  }else{
  6.   if(obj.addEventListener)
  7.    obj.addEventListener(tipo, toDo, false);
  8.   else
  9.    obj['on'+tipo]=toDo;
  10.  }
  11. }
PD: No creo que me deje darte karma, pero aún así tienes todo mi agradecimiento (:
Edit: No, no me deja darte más karma -.-'
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #10 (permalink)  
Antiguo 29/01/2011, 16:14
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Problema con objeto en IE

de hecho es una buena practica, yo siempre lo hago. aunque no uso el tercer valor. hay alguna diferencia??
  #11 (permalink)  
Antiguo 29/01/2011, 17:14
Avatar de buzu  
Fecha de Ingreso: octubre-2006
Ubicación: San Francisco, CA
Mensajes: 2.168
Antigüedad: 17 años, 6 meses
Puntos: 122
Respuesta: Problema con objeto en IE

De hecho attachEvent solo acepta dos parametros:
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Ahora que alguien explique que rayos hace ese false ahí. Mi teoría es que alguien simplemente lo copió de addEventListener, que si acepta un tercer parámetro y este hace gran diferencia.
Ahora, no hay necesidad de re-inventar la rueda, a menos que la vallas a mejorar y este no es el caso. Mejor usa addEvent de Dustin Diaz:
http://www.dustindiaz.com/rock-solid-addevent/
__________________
twitter: @imbuzu
  #12 (permalink)  
Antiguo 29/01/2011, 17:57
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

Cita:
Iniciado por buzu Ver Mensaje
addEventListener, que si acepta un tercer parámetro y este hace gran diferencia.
¿Qué diferencia hay entre usar true y false en addEventListener?
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #13 (permalink)  
Antiguo 29/01/2011, 18:04
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

https://developer.mozilla.org/en/DOM...dEventListener
__________________
http://es.phptherightway.com/
thats us riders :)
  #14 (permalink)  
Antiguo 29/01/2011, 18:20
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema con objeto en IE

La verdad es que ya había estado en esa página, pero mi nivel de inglés no es tan alto ¿La traducción al español sería ...? XD
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #15 (permalink)  
Antiguo 29/01/2011, 18:34
Avatar de buzu  
Fecha de Ingreso: octubre-2006
Ubicación: San Francisco, CA
Mensajes: 2.168
Antigüedad: 17 años, 6 meses
Puntos: 122
Respuesta: Problema con objeto en IE

Para entender la diferencia primero tienes que entender el tema de los burbujeos. El único sitio que conozco que explica eso a detalle está en inglés, pero puedes leer esto para darte una idea:
http://es.wikipedia.org/wiki/Modelo_...s_de_Documento
Siempre es importante tener en cuenta la propagación de los eventos, especialmente cuando se quieren aplicar modelos como la delegación de eventos la cual resulta muy útil en sitios que usan eventos en gran medida.
__________________
twitter: @imbuzu
  #16 (permalink)  
Antiguo 29/01/2011, 18:45
Avatar de masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con objeto en IE

Te dejo una referencia en español http://www.versiontexto.com/event-bu...ent-capturing/, al final aparecen las referencias a quirksmode, este articulo esta muy bueno lamentablemente al igual que la mayoria de la buena info esta en ingles, Events Order QuirksMode.
__________________
http://es.phptherightway.com/
thats us riders :)

Etiquetas: objeto
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:19.