Foros del Web » Programando para Internet » Jquery »

¿puedo combinar load y resize?

Estas en el tema de ¿puedo combinar load y resize? en el foro de Jquery en Foros del Web. Tengo el siguiente código donde repito el código del iframe 2 veces, me gustaría saber si es posible optimizar este código a la mitad de ...
  #1 (permalink)  
Antiguo 09/04/2014, 07:17
 
Fecha de Ingreso: enero-2008
Mensajes: 580
Antigüedad: 16 años, 3 meses
Puntos: 9
¿puedo combinar load y resize?

Tengo el siguiente código donde repito el código del iframe 2 veces, me gustaría saber si es posible optimizar este código a la mitad de lineas, gracias

Código HTML:
	    $('iframe~*').remove();
      $('iframe').attr('width', '100%').attr('height', $('iframe').width()/16*9).each(function(){
          var url = $(this).attr("src");
          var char = "?";
          if(url.indexOf("?") != -1){
              var char = "&";
          }
          $(this).attr("src",url+char+"wmode=transparent");
      });
      $(window).resize(function(e) {
        $('iframe').attr('width', '100%').attr('height', $('iframe').width()/16*9).each(function(){
            var url = $(this).attr("src");
            var char = "?";
            if(url.indexOf("?") != -1){
                var char = "&";
            }
            $(this).attr("src",url+char+"wmode=transparent");
        });
      });
  #2 (permalink)  
Antiguo 13/04/2014, 00:15
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: ¿puedo combinar load y resize?

Podrías colocar el código en una función y luego solamente invocarías a la función en cada método.

Código Javascript:
Ver original
  1. var miFuncion = function(){
  2.     $('iframe').attr('width', '100%').attr('height', $('iframe').width()/16*9).each(function(){
  3.         var url = $(this).attr("src"),
  4.             char = "?";
  5.  
  6.         if(url.indexOf("?") != -1)
  7.             char = "&";
  8.        
  9.         $(this).attr("src",url+char+"wmode=transparent");
  10.     });
  11. };
  12.  
  13. $(document).ready(function(){
  14.     miFuncion();
  15. });
  16.  
  17. $(window).resize(function(){
  18.     miFuncion();
  19. });

Saludos
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand

Etiquetas: combinar, load
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 23:55.