Tema: Cookies
Ver Mensaje Individual
  #19 (permalink)  
Antiguo 15/06/2010, 13:22
Avatar de _cronos2
_cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Cookies

Nada, sigue sin funcionarme. Ya he conseguido que pasando de una página a otra aparezca 'Hola hoy es día tal y son las...', pero no me sale el valor de la cookie. Posteo el código a ver qué se puede hacer, la verdad que ya estoy un poco desesperado

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. //prin.html, es la primera que sale y donde se tienen que coger las cookies
  3. function getCookie(nombreCookie){
  4.  if (document.cookie.length>0){
  5.   c_start=document.cookie.indexOf(nombreCookie + "=");
  6.   if (c_start!=-1){
  7.    c_start=c_start + nombreCookie.length+1;
  8.    c_end=document.cookie.indexOf(";",c_start);
  9.    if (c_end==-1){
  10.     c_end=document.cookie.length;
  11.    }   
  12.    return unescape(document.cookie.substring(c_start,c_end));
  13.   }
  14.  }
  15.  return "";
  16. }
  17. function setCookie(nombreCookie,valorCookie,expiredays){
  18.  var fecha=new Date();
  19.  fecha.setDate(fecha.getDate()+expiredays);
  20.  document.cookie=nombreCookie+ "=" +escape(valorCookie) + ((expiredays==null) ? "" : ";expires="+fecha.toUTCString())+"; path=/";
  21. }
  22. function reloj(){
  23.  var f = new Date();
  24.  var a = f.getFullYear();  
  25.  var m = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
  26.  var d = new Array ("Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado");
  27.  var h = f.getHours();
  28.  var mi = f.getMinutes();
  29.  var s = f.getSeconds();
  30.  if (h<10){h='0'+h}
  31.  if (h>12){h=h-12}
  32.  if (mi<10){mi='0'+mi}
  33.  if (s<10){s='0'+s}
  34.  document.getElementById("capaPersonal").innerHTML = '<span style="font-size:25px;">Hola,&nbsp;</span><span style="font-size:30px; text-transform: capitalize;">' + nUsuario + '</span><span class="fecha">Hoy es ' + d[f.getDay()] + ', ' + f.getDate() + ' de ' + m[f.getMonth()] + ' de ' + a + ' y son las ' + h + ':' + mi + ':' + s + '</span>';
  35. }
  36. function checkCookie(){
  37.  nUsuario=getCookie('usuario');
  38.  if(nUsuario==null || nUsuario==""){
  39.   nUsuario=prompt('Por favor, introduce tu nombre',"");
  40.  }else{
  41.   setCookie('usuario',nUsuario,1);
  42.  }
  43.  setInterval("reloj()", 1000);
  44. }
  45. </script>

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. //formulario.html, aquí tendría que aparecer arriba lo mismo que en prin.html
  3. function getCookie(nombreCookie){
  4.  if (document.cookie.length>0){
  5.   c_start=document.cookie.indexOf(nombreCookie + "=");
  6.   if (c_start!=-1){
  7.    c_start=c_start + nombreCookie.length+1;
  8.    c_end=document.cookie.indexOf(";",c_start);
  9.    if (c_end==-1){
  10.     c_end=document.cookie.length;
  11.    }   
  12.    return unescape(document.cookie.substring(c_start,c_end));
  13.   }
  14.  }
  15.  return "";
  16. }
  17. function setCookie(nombreCookie,valorCookie,expiredays)
  18. {
  19. var fecha=new Date();
  20. fecha.setDate(fecha.getDate()+expiredays);
  21. document.cookie=nombreCookie+ "=" +escape(valorCookie)+
  22. ((expiredays==null) ? "" : ";expires="+fecha.toUTCString())+"; path=/";
  23. }
  24.  
  25. function checkCookie(){
  26.  if(document.cookie.length>0){
  27.   nUsuario=getCookie('usuario');
  28.   if(nUsuario!=null || nUsuario!=""){
  29.    setCookie('usuario',nUsuario,1);
  30.    setInterval("reloj()", 1000);
  31.   }else{
  32.    document.getElementById("capaPersonal").style.display = 'none';
  33.   }
  34.  }  
  35. }
  36.  
  37. function reloj(){
  38.  var f = new Date();
  39.  var a = f.getFullYear();  
  40.  var m = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
  41.  var d = new Array ("Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado");
  42.  var h = f.getHours();
  43.  var mi = f.getMinutes();
  44.  var s = f.getSeconds();
  45.  if (h<10){h='0'+h}
  46.  if (h>12){h=h-12}
  47.  if (mi<10){mi='0'+mi}
  48.  if (s<10){s='0'+s}
  49.  document.getElementById("capaPersonal").innerHTML = '<span style="font-size:25px;">Hola,&nbsp;</span><span style="font-size:30px; text-transform: capitalize;">' + nUsuario + '</span><span class="fecha">Hoy es ' + d[f.getDay()] + ', ' + f.getDate() + ' de ' + m[f.getMonth()] + ' de ' + a + ' y son las ' + h + ':' + mi + ':' + s + '</span>';
  50. }
  51. </script>

Código HTML:
Ver original
  1. <!---prin.html--->
  2. Si quieres una experiencia más personal de la página, haz click <a href='#' onclick='checkCookie();' class='enlace'>aquí</a>
  3. <!---formulario-html--->
  4. <body onload='checkCookie();'>

Tengo más páginas pero con saber cómo hay que hacerlo con estas dos me servirá para las demás.
Saludos y gracias (: