Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/05/2013, 12:41
perro123
 
Fecha de Ingreso: marzo-2013
Mensajes: 21
Antigüedad: 11 años, 1 mes
Puntos: 0
Respuesta: Por que este codigo javascript no anda a la noche

Cita:
Iniciado por emprear Ver Mensaje
Porque tu última condición no se cumple, supone que son las 23hs

if((today.getHours() >=21) && (today.getHours() <=6)){

es mayor ó igual a 21 --> SI

y (&&)

es menor ó igual a 6 --> NO

Debería ser asi (esta extraido de otro ejemplo que puse en el foro)


Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>titulo</title> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="estilos1.css" media="screen" id="hojadeestilos" />
<script type="text/javascript">
//<![CDATA[
function banner() {
var hoy = new Date();
var hora = hoy.getHours();
var banner_logo = document.getElementById('banner');
if((hora >= 7)&&(hora <= 14)) { // 7,8,9,10,11,12,13,14
banner_logo.src = 'sfondo03.jpg';
document.getElementById('hojadeestilos').href='estilos1.css';
} 
if((hora >= 15)&&(hora <= 22)) { // 15,16,17,18,19,20,21,22
banner_logo.src = 'sfondo07.jpg';
document.getElementById('hojadeestilos').href='estilos2.css';
}
if((hora >= 23)||(hora < 7)){ // 23,0,1,2,3,4,5,6
banner_logo.src = 'sfondo06.jpg'; 
document.getElementById('hojadeestilos').href='estilos3.css';
}
}
//]]>
</script>
</head>
<body onload="banner();">
<h1>Demo</h1>
<img src="sfondo03.jpg" id="banner" alt="" />   
</body>
</html> 
Código:
if((hora >= 23)||(hora < 7)){ // 23,0,1,2,3,4,5,6 
útilizo OR (||)
ves la diferencia

SAludos
MUCHAS GRACIAS, TAMBIEN SIMPLIFIQUE EL CODIGO, ANDA MUY BIEN

Código HTML:
<script language="JavaScript">
day = new Date()
hr = day.getHours()
if ((hr>= 6)&&(hr <= 12)){document.write("<img src=tucarpeta/tu-imagen.png width=260 height=260>")}
if ((hr>= 13)&&(hr <= 18)){document.write("<img src=tucarpeta/tu-imagen.png width=260 height=260>")}
if ((hr>= 19)&&(hr <= 20)){document.write("<img src=tucarpeta/tu-imagen.png width=260 height=260>")}
if ((hr>= 21)||(hr < 6)){document.write("<img src=tucarpeta/tu-imagen.png width=260 height=260>")}
</script>