Problems calling a JavaScript function in a buttom in html! [closed]

0

I created two functions in js for two buttons in Html, one reads the name of visitors to the site and the other shows the name of the visitors who already read, as follows:

var nomearray= [ ];

function nome(){

  var nome = prompt("Olá viajante, digite seu nome:")
  var cont = cont + 1;
  nomearray[0 + cont] = nome;  
}

function totalVisitantes(){

  for(var i=0; i<=cont;i++){
    alert(nomearray[i])
  }
}

Obs : I declared the array outside of the functions to be global and to be able to be accessed in the other function.

In html, I called them like this:

< button onclick="nome()" >Qual seu nome?< /button >

< button onclick="totalVisitantes()">Visitantes< /button >

Obs : the button's are in < head >.

What would be the reason for the button's not being able to call the functions? The grid is the help of everyone beforehand. If my explanation has been confusing, I apologize.

<html >
    <head >
        <title>Dark Site</title >
        <link rel="stylesheet" href="estilo1.css" >
        <script type="text/javascript" scr="script1.js"></script >

        <script language="javascript"> 
            function setCookie(nome, valor, dias){ 
            diasms = (new Date()) .getTime() + 1000 * 3600 * 24 * dias; 
            dias = new Date(diasms); 
            expires = dias.toGMTString(); 
            document.cookie = escape(nome) + "=" + escape(valor) + "; expires=" + expires; 
            } 
            </script >

    </head >
        <body >
                <button onclick="nome()">Qual seu nome? </button >

                <button onclick="totalVisitantes()">Visitantes</button >

            <h1><font color="white"><center>Historias Macabras</center ></font></h1>

            <script language="javascript" >
                if (!document.cookie){
                setCookie("cookie", "1", 365);
                document.write("<font face='verdana' size='1'>Visitas : 1</font>");
                } else {
                var cont = document.cookie;
                var dividindo = cont.split("=");
                //document.write(dividindo[1]);
                var numero = parseInt(dividindo[1]);
                var soma = numero + 1;
                document.write("<font face='verdana' size='1'>Visitas : " + soma + "</font>");
                setCookie("cookie", soma, 365);


                }
                </script> 

            <style type="text/css">
                img{border-radius: 20px;}
                </style>

            <h2><a  href="https://maringapost.com.br/ahduvido/10-historias-classicas-da-creepypasta/"><center><font color="red">A Estátua</font></center></a></h2>

            <table>
                <tr>

                <td><img src="1.jpg" width="300" height="250" alt="A Estátua" /></td>
                <td><font color="grey"><center></center>"texto"</center></font></td >

                </tr >
            </table>

            <h2><a  href="https://maringapost.com.br/ahduvido/10-historias-classicas-da-creepypasta/"><center><font color="red">A Carta</font></center></a></h2>

            <table>
                <tr>
                    <td><img src="2.jpg" width="300" height="250" alt="O Açogueiro"> </td>
                    <td><font color="grey"><center></center>"texto”</center></font></td>
                </tr >
            </table >

            <table >
                <tr>
                    <h2><a  href="https://maringapost.com.br/ahduvido/10-historias-classicas-da-creepypasta/"><center><font color="red">Espelhos</font></center></a></h2>
                    <td><img src="3.jpg" width="300" height="250" alt="O Açogueiro"></td>
                    <td><font color="grey"><center></center>"texto”</center></font></td>
                </tr>
            </table>

            <table >
                <tr>
                    <h2><a  href="https://maringapost.com.br/ahduvido/10-historias-classicas-da-creepypasta/"><center><font color="red">O celular</font></center></a></h2>
                    <td><img src="4.jpg" width="300" height="250" alt="O Celular Novo"></td>
                    <td><font color="grey"><center></center>"texto".</center></font></td>
                </tr>
            </table >

            <center><table >
                <tr>
                    <td><embed src="song1.mp3" width="50" height="90" autostart="true"></td>
                        <td><h2><a href="beguin3.html">images</a>2</h2></td>
                        <td><h2><a href="file:///C:/Users/douglas/Desktop/DarkSite/beguin1.html">home</a></h2></td>

                     <a href="file:///C:/Users/douglas/Desktop/DarkSite/beguin2.html">1</a></h3></td>
                </tr>
            </table></center>
        </body>
</html>

Note: Please ignore the texts.

    
asked by anonymous 15.12.2018 / 22:53

1 answer

1

As you can see in my example, your code is working normally, I just made some changes to points that were not correct:

  You have to declare the variable cont also outside the nome() function because you are using totalVisitants () .

/ p>      

2 - It is not very appropriate to use variable name with the same function name as you did with name .

     

3 - As nomearray is a vector , you will need to use the to save the values.

var nomearray = [];
var cont = 0;

function nome() {

  var nomes = prompt("Olá viajante, digite seu nome:");
  nomearray.push(nomes);
  cont++;
  console.log(cont);
}

function totalVisitantes(){

  for(var i=0; i<cont ;i++){
    alert(nomearray[i]);
  }
}
<button onclick="nome()">Qual seu nome? </button >

<button onclick="totalVisitantes()">Visitantes</button >
    
15.12.2018 / 23:39