Avascript: Reading and consulting and recording cookies

Asked

Viewed 371 times

1

The start screen shows a lamp out. The image of the lamp was found in:

http://imagensxti.xpg.uol.com.br/img/xlamp_off.png

Also shows a time display mask.

Follow below, the page code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>JavaScript - Cookies </title>
        <style type="text/css">
            body { background-color:#000; }
            #xti { display:block; margin:20px auto; }
            #horas {color:#fff; margin:0 auto; width:110px; font-size:30px};
        </style>
        <script type="text/javascript" src="js/cookie.js"></script>
        <script type="text/javascript" src="js/Aula39.js"></script>
    </head>
    <body>
        <img id="xti"  
            src="img/xlamp_off.png" />

        <div id="horas">00:00:00</div>

    </body>
</html>

With a mouse click on top of the figure, activates the function connect, the following should occur:

In the Aula39.js script file, the call function displays a picture of an access lamp found in:

http://imagensxti.xpg.uol.com.br/img/xlamp_on.png

The function mostrarHoras displays the current time at 1 second intervals;

In the coockie.js file, readCookie function checks the existence of coockie by sending a greeting if there is a user or a request for user name input, as per the following script:

window.onload = function() {
document.getElementById("xti").onclick = ligar;
setInterval(mostrarHoras, 1000);

var nome = readCookie("nome");
    if(nome == null) {
    nome = prompt("Qual o seu nome?");
    writeCookie("nome", nome, 1);
    }
}

function ligar() {
document.getElementById("xti").src = "img/xlamp_on.png";

    var nome = readCookie("nome");
        if(nome != null){
            alert("Como vai? " + nome);
    }

setTimeout(desligar, 3000);
}

function desligar() {
document.getElementById("xti").src = "img/xlamp_off.png";
}

function mostrarHoras() {
var agora = new Date();
var horas = agora.getHours() + ":" + agora.getMinutes() +  ":" + agora.getSeconds();
document.getElementById("horas").innerHTML = horas;
}

The function readCookie is being considered as undefined and the others relating to coockie, as the following script in the coockie.js file, are not being executed.

function writeCookie(name, value, days){
//Por default, nâo existe expiração, ou seja o cookie é temporário
var expires = "";

//Especifica o número de dias para guardar o cookie
if (days) {
var date = new Date();
date.setTime(date.getTime)() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}

if(value != "" && value != null && value != "null"){
//Define o cookie com o nome, valor e data de expiração
document.cookie = name + "=" + value + expires + "; path=/";
}
}

function  (name) {
//Encontra o cookie especificado e retorna o seu valor
var searchName = name + "=";
var cookies = document.cookie.split(';');
for(var i=0; i < cookies.length; i++) {
var c = cookies [i];
while (c.chartAt(0) == ' ')
c = c.substring(1, c.length);
if (c.indexOf(searchName) == 0)
   return c.substring(searchName.length, c.length);
  }
  return null;
}

function eraseCookie(name){  
//Exclui o cookie
  writeCookie(name, "", -1);
  }

I would like that if anyone knows what could be the reason, help me to continue the task. I am grateful.

  • The error is precisely in the file cookie.js that this without the function name function (name) { => function readCookie(name) {

  • Now you are giving error " Uncaught Typeerror: c.chartAt is not a Function" in the cookie line.js:24. --> while (c.chartAt(0) == ' ') Lamp does not go out.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.