Ventana de eventos JavaScript.onload no activado


Tengo el siguiente código en un sitio web:

 window.onload = resize;
 window.onresize = resize;

 function resize(){
  heightWithoutHeader = (window.innerHeight - 85) + "px"; 
  document.getElementById("main-table").style.height = heightWithoutHeader;
  document.getElementById("navigation").style.height = heightWithoutHeader;
 }

El onresize funciona bien, pero el evento onload nunca se dispara. Lo he probado en Firefox y Chrome y ninguno de ellos funciona.

Gracias por su ayuda y vaya por la reputación! ; D

Author: Nick Craver, 2010-05-11

7 answers

Creo que lo que probablemente está sucediendo aquí es que su window.onload se anula más tarde, verifique para asegurarse de que no sea a través de cosas como <body onload="">

Puede comprobar esto por alert(window.onload) en su función de re-tamaño, para ver lo que realmente se adjunta allí.

 61
Author: Nick Craver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-11 14:31:00

Esto sucedió cuando agregué código jQuery de terceros que necesitábamos para un socio. Podría haber convertido fácilmente mi ventana anticuada.onload a un documento de jQuery listo. Dicho esto, quería saber si hay un día moderno, solución compatible con el navegador cruzado.

Hay!

window.addEventListener ? 
window.addEventListener("load",yourFunction,false) : 
window.attachEvent && window.attachEvent("onload",yourFunction);

Sé que sé que, puedo convertir mi código para usar la ruta jQuery. Y le pediré a nuestro socio que refactorice su código para que dejen de afectar a los sitios.

Fuente donde encontré la solución --> http://ckon.wordpress.com/2008/07/25/stop-using-windowonload-in-javascript /

 21
Author: Allen Hurff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-15 18:33:02

Mueve la ventana.onload línea al final del archivo javascript o después de la función inicial y funcionará:

function resize(){
    heightWithoutHeader = (window.innerHeight - 85) + "px"; 
    document.getElementById("main-table").style.height = heightWithoutHeader;
    document.getElementById("navigation").style.height = heightWithoutHeader;
}
// ...
// at the end of the file...
window.onload = resize;
window.onresize = resize;

Pero es una buena práctica si no reemplaza el onload también. En su lugar, adjunte su función al evento onload:

function resize(){
    heightWithoutHeader = (window.innerHeight - 85) + "px"; 
    document.getElementById("main-table").style.height = heightWithoutHeader;
    document.getElementById("navigation").style.height = heightWithoutHeader;
}
// ...
// at the end of the file...
window.addEventListener ? 
    window.addEventListener("load",resize,false) 
    : 
    window.attachEvent && window.attachEvent("onload",resize);

Eso funcionó para mí y lo siento por mi inglés.

 2
Author: august0490,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-30 04:32:44

Esto funciona para mí, creo que su problema está en otro lugar:

 function resize(){
  var tester = document.getElementById("tester"),
      html = tester.innerHTML

  tester.innerHTML = html + "resize <br />"
 }  

window.onload = resize;
window.onresize = resize;

Puede probarlo usted mismo aquí: http://jsfiddle.net/Dzpeg/2 /

¿Estás seguro de que es el único evento llamado onLoad ? Tal vez otro evento onLoad crea un conflicto

 1
Author: meo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-11 13:17:51

Esto funcionará cuando llame a la "ventana.onload " junto a la función resize()

 0
Author: Prabhu M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-11 13:06:58

Para mí, window.onload no estaba funcionando cuando se escribió dentro de la etiqueta script type="text/javascript.

En su lugar, necesitaba escribir lo mismo en la etiqueta script language="Javascript" type="text/javascript y funcionó bien.

 0
Author: theUltimateWinner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-01 11:48:40

Si realmente está en ese orden, definitivamente no va a funcionar. No se puede asignar una función a un controlador de eventos antes de que se declare la propia función.

 -3
Author: Syntactic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-11 12:58:25