Foros del Web » Programando para Internet » Javascript »

problemas con window.onload

Estas en el tema de problemas con window.onload en el foro de Javascript en Foros del Web. Hola que tal, hora acabo de implementar otro script en una pagina que tengo y funcionaba bien pero a la hora de poner el otro ...
  #1 (permalink)  
Antiguo 25/06/2010, 19:24
 
Fecha de Ingreso: agosto-2009
Mensajes: 292
Antigüedad: 14 años, 8 meses
Puntos: 5
problemas con window.onload

Hola que tal, hora acabo de implementar otro script en una pagina que tengo y funcionaba bien pero a la hora de poner el otro script que tiene un windows.onload dejo de funcionar la function countCheckboxes

Alguien sabe como solucionar esto

Código HTML:
Ver original
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <title>Untitled Document</title>
  3.  
  4.     <script type="text/javascript">
  5.     function countCheckboxes()
  6.     {
  7.             var form = document.getElementById('album');
  8.             var count = 0;
  9.             for(var n = 0; n < form.length; n++)
  10.             {
  11.                if(form[n].name == 'addAlbum[]' && form[n].checked)
  12.                {
  13.                    count++;
  14.                }
  15.             }
  16.             document.getElementById('checkCount').innerHTML = count;
  17.     }
  18.        </script>
  19.  
  20. <script type="text/javascript">
  21. //This Function Creates your Cookie for you just pass in the Cookie Name, Value, and number of days before you want it to expire.
  22. function CreateCookie(name,value,days)
  23. {
  24.     if (days)
  25.     {
  26.         var date = new Date();
  27.         date.setTime();
  28.         var expires = "; expires="+date.toGMTString();
  29.     }
  30.     else var expires = "";
  31.     document.cookie = name + "=" + value + expires + "; path=/";
  32. }
  33.  
  34. //This Function reads the value of a given cookie for you.  Just pass in the cookie name and it will return the value.
  35. function ReadCookie(name)
  36. {
  37.     var nameEQ = name + "=";
  38.     var ca = document.cookie.split(';');
  39.     for(var i=0;i < ca.length;i++) {
  40.         var c = ca[i];
  41.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  42.         if (c.indexOf(nameEQ) == 0)
  43.        {
  44.            return c.substring(nameEQ.length,c.length);
  45.        }
  46.     }
  47.     return null;
  48. }
  49.  
  50. //This Function Erases Cookies.  Just pass in the name of the cookies you want erased.
  51. function EraseCookie(name)
  52. {
  53.     CreateCookie(name,"",-1);
  54. }
  55.  
  56. //Sets or UnSets Cookies for given checkbox after it's been clicked on/off.
  57. function ChangeBox(CheckBox)
  58. {
  59.     if (document.getElementById(CheckBox).checked)
  60.     {
  61.         var CurrentCookie = ReadCookie("checkimg");
  62.         CurrentCookie = CurrentCookie + CheckBox;  
  63.         CreateCookie("checkimg",CurrentCookie,"100");
  64.     }
  65.     else
  66.     {
  67.         var CurrentCookie = ReadCookie("checkimg");
  68.         CurrentCookie = CurrentCookie.replace(CheckBox,"");
  69.         CreateCookie("checkimg",CurrentCookie,"100");
  70.     }
  71. }
  72. //Runs on body load to check history of checkboxes on the page.
  73. function CheckCookies()
  74. {
  75.     var CurrentCookie = ReadCookie("checkimg");
  76.     for (i=0; i<document.CheckList.elements.length; i++)
  77.     {
  78.         if (document.CheckList.elements[i].type == "checkbox")
  79.         {
  80.             document.CheckList.elements[i].onclick = function() {ChangeBox(this.id);};
  81.            if (CurrentCookie && CurrentCookie.indexOf(document.CheckList.elements[i].id) > -1)
  82.             {
  83.                 document.CheckList.elements[i].checked = true;
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. //Clears Form
  90. function ClearBoxes()
  91. {
  92.     for (i=0; i<document.CheckList.elements.length; i++)
  93.     {
  94.         if (document.CheckList.elements[i].type == "checkbox")
  95.         {
  96.             document.CheckList.elements[i].checked = false;
  97.             ChangeBox(document.CheckList.elements[i].id);
  98.         }
  99.     }
  100. }
  101. window.onload = CheckCookies;
  102.  
  103. </head>

Saludos y gracias de antemano
  #2 (permalink)  
Antiguo 25/06/2010, 20:05
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: problemas con window.onload

no veo donde conflige el evento window.onload. en el primer script tienes una funcion que cuenta los checkboxes marcados. en el segundo script tienes funciones para manejar cookies, me parece que en base a unas opciones marcadas. pero solo en este ultimo veo asignado el evento onload, el cual tampoco veo mal. en todo caso tampoco se ve cuando invocas countCheckboxes. ¿queras explicarte con un ejemplo mas completo?
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #3 (permalink)  
Antiguo 25/06/2010, 20:28
 
Fecha de Ingreso: agosto-2009
Mensajes: 292
Antigüedad: 14 años, 8 meses
Puntos: 5
Respuesta: problemas con window.onload

Hola que tal zerokilled, el countCheckboxes no lo puse anteriormente, pero lo invoco asi <div id="checkCount">0</div>, aqui esta el codigo completo

Código HTML:
Ver original
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <title>Untitled Document</title>
  3.  
  4.     <script type="text/javascript">
  5.     function countCheckboxes()
  6.     {
  7.             var form = document.getElementById('album');
  8.             var count = 0;
  9.             for(var n = 0; n < form.length; n++)
  10.             {
  11.                if(form[n].name == 'addAlbum[]' && form[n].checked)
  12.                {
  13.                    count++;
  14.                }
  15.             }
  16.             document.getElementById('checkCount').innerHTML = count;
  17.     }
  18.        </script>  
  19.  
  20.        
  21.         <script type="text/javascript">
  22. //This Function Creates your Cookie for you just pass in the Cookie Name, Value, and number of days before you want it to expire.
  23. function CreateCookie(name,value,days)
  24. {
  25.     if (days)
  26.     {
  27.         var date = new Date();
  28.         date.setTime(date.getTime()+(days*24*60*60*1000));
  29.         var expires = "; expires="+date.toGMTString();
  30.     }
  31.     else var expires = "";
  32.     document.cookie = name + "=" + value + expires + "; path=/";
  33. }
  34.  
  35. //This Function reads the value of a given cookie for you.  Just pass in the cookie name and it will return the value.
  36. function ReadCookie(name)
  37. {
  38.     var nameEQ = name + "=";
  39.     var ca = document.cookie.split(';');
  40.     for(var i=0;i < ca.length;i++) {
  41.         var c = ca[i];
  42.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  43.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  44.     }
  45.     return null;
  46. }
  47.  
  48. //This Function Erases Cookies.  Just pass in the name of the cookies you want erased.
  49. function EraseCookie(name)
  50. {
  51.     CreateCookie(name,"",-1);
  52. }
  53.  
  54. //Sets or UnSets Cookies for given checkbox after it's been clicked on/off.
  55. function ChangeBox(CheckBox)
  56. {
  57.     if (document.getElementById(CheckBox).checked)
  58.     {
  59.         var CurrentCookie = ReadCookie("fabletwomaps");
  60.         CurrentCookie = CurrentCookie + CheckBox;  
  61.         CreateCookie("fabletwomaps",CurrentCookie,"100");
  62.     }
  63.     else
  64.     {
  65.         var CurrentCookie = ReadCookie("fabletwomaps");
  66.         CurrentCookie = CurrentCookie.replace(CheckBox,"");
  67.         CreateCookie("fabletwomaps",CurrentCookie,"100");
  68.     }
  69. }
  70. //Runs on body load to check history of checkboxes on the page.
  71. function CheckCookies()
  72. {
  73.     var CurrentCookie = ReadCookie("fabletwomaps");
  74.     for (i=0; i<document.CheckList.elements.length; i++)
  75.     {
  76.         if (document.CheckList.elements[i].type == "checkbox")
  77.         {
  78.             document.CheckList.elements[i].onclick = function() {ChangeBox(this.id);};
  79.            if (CurrentCookie && CurrentCookie.indexOf(document.CheckList.elements[i].id) > -1)
  80.             {
  81.                 document.CheckList.elements[i].checked = true;
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. //Clears Form
  88. function ClearBoxes()
  89. {
  90.     for (i=0; i<document.CheckList.elements.length; i++)
  91.     {
  92.         if (document.CheckList.elements[i].type == "checkbox")
  93.         {
  94.             document.CheckList.elements[i].checked = false;
  95.             ChangeBox(document.CheckList.elements[i].id);
  96.         }
  97.     }
  98. }
  99.  
  100. window.onload=CheckCookies;
  101.  
  102.  
  103.        
  104. </head>
  105.  
  106.  
  107. 1 - 12 de 124 imagenes.
  108.  
  109. <form action="compress.php" method="post" id="album" name="CheckList">  
  110. <div id="albumList">
  111.         <div class="albumItem"><div class="albumImg"><img src='img/filter_closed.png' alt='filter_closed.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='filter_closed.png' value='filter_closed.png' onclick='countCheckboxes()' />filter_closed.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=filter_closed.png"></a></div></div>
  112.         <div class="albumItem"><div class="albumImg"><img src='img/folder_up.png' alt='folder_up.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='folder_up.png' value='folder_up.png' onclick='countCheckboxes()' />folder_up.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=folder_up.png"></a></div></div>
  113.         <div class="albumItem"><div class="albumImg"><img src='img/transfer_upload.png' alt='transfer_upload.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='transfer_upload.png' value='transfer_upload.png' onclick='countCheckboxes()' />transfer_upload.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=transfer_upload.png"></a></div></div>
  114.         <div class="albumItem"><div class="albumImg"><img src='img/fitwidth_enabled.png' alt='fitwidth_enabled.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='fitwidth_enabled.png' value='fitwidth_enabled.png' onclick='countCheckboxes()' />fitwidth_enabled.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=fitwidth_enabled.png"></a></div></div>
  115.         <div class="albumItem"><div class="albumImg"><img src='img/dropup.png' alt='dropup.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='dropup.png' value='dropup.png' onclick='countCheckboxes()' />dropup.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=dropup.png"></a></div></div>
  116.  
  117.         <div class="albumItem"><div class="albumImg"><img src='img/search-suggestion.png' alt='search-suggestion.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='search-suggestion.png' value='search-suggestion.png' onclick='countCheckboxes()' />search-suggestion.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=search-suggestion.png"></a></div></div>
  118.         <div class="albumItem"><div class="albumImg"><img src='img/search-suggestion-selected.png' alt='search-suggestion-selected.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='search-suggestion-selected.png' value='search-suggestion-selected.png' onclick='countCheckboxes()' />search-suggestion-selected.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=search-suggestion-selected.png"></a></div></div>
  119.         <div class="albumItem"><div class="albumImg"><img src='img/mail_status_disconnected.png' alt='mail_status_disconnected.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='mail_status_disconnected.png' value='mail_status_disconnected.png' onclick='countCheckboxes()' />mail_status_disconnected.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=mail_status_disconnected.png"></a></div></div>
  120.         <div class="albumItem"><div class="albumImg"><img src='img/group_closed.png' alt='group_closed.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='group_closed.png' value='group_closed.png' onclick='countCheckboxes()' />group_closed.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=group_closed.png"></a></div></div>
  121.         <div class="albumItem"><div class="albumImg"><img src='img/archives.png' alt='archives.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='archives.png' value='archives.png' onclick='countCheckboxes()' />archives.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=archives.png"></a></div></div>
  122.         <div class="albumItem"><div class="albumImg"><img src='img/block.png' alt='block.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='block.png' value='block.png' onclick='countCheckboxes()' />block.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=block.png"></a></div></div>
  123.  
  124.         <div class="albumItem"><div class="albumImg"><img src='img/images_cached.png' alt='images_cached.png'/></div><div class="albumCheck"><label><input type='checkbox' name='addAlbum[]' id='images_cached.png' value='images_cached.png' onclick='countCheckboxes()' />images_cached.png</label></div><div class="albumDownload"><a href="http://psy-tribe.co.cc/download.php?file=images_cached.png"></a></div></div>
  125.          
  126. </div>
  127.  
  128. <span id="checkCount"></span>
  129.  
  130. <input type="submit" name="Submit" value="Enviar">
  131. </form>
  132.  
  133. </body>
  134.  
  135. </html>

Saludos
  #4 (permalink)  
Antiguo 25/06/2010, 20:49
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: problemas con window.onload

la razon por la que no se invoca countCheckboxes es porque estas sobreescribiendo el evento. es decir, en primera instancia el evento esta registrado como atributo. pero luego, en la funcion CheckCookies estas registrando el mismo evento pero con instrucciones distintas. ¿que puedes hacer? en lugar de registrar el evento como atributo, agrega la invocacion de countCheckboxes en la registracion del evento dentro de la funcion CheckCookies.
Código:
element.onclick = function(){
// aqui invocas countCheckboxes;
// el resto de las instrucciones;
}
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.

Etiquetas: Ninguno
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 21:19.