Redirect in javascript does not work with external script

0

Redirect in javascript does not work with external script.

I have a page with the following code:

<input type="text" name="login" id="login">
<input type="password" name="senha" id="senha">
<button type="button" id="btnEntrar">Entrar</button>
<script src="js/functions-index.js"></script>

On my page functions-index.js:

        var btnEntrar = document.getElementById('btnEntrar');
        var login = document.getElementById('login');
        var senha = document.getElementById('senha');


        btnEntrar.addEventListener('click', function(){

          if(login.value ==''){
            alert("Preencha o campo email");
          }
          else if(login.senha == ''){
            alert("Preencha o campo senha");
          }
          else if(login.senha === '123'){
                console.log(login.value);
                location.href = "pag2.html";


          }


 });

However, redirection does not work. Even with href , location , replace , and so on No error is returned

    
asked by anonymous 17.05.2017 / 23:33

2 answers

0

HTML

<input type="text" name="login" id="login">
<input type="password" name="senha" id="senha">
<button type="button" id="btnEntrar">Entrar</button>
<script src="js/functions-index.js"></script>

JavasScript

Close script is missing with });

Change login.senha by senha.value

    var btnEntrar = document.getElementById('btnEntrar');
    var login = document.getElementById('login');
    var senha = document.getElementById('senha');


   btnEntrar.addEventListener('click', function(){

      if(login.value ==''){
        alert("Preencha o campo email");
      }
      else if(senha.value == ''){
        alert("Preencha o campo senha");
      }
      else if(senha.value === '123'){
            //console.log(login.value);
            alert(login.value);
            location.href = "pag2.html";


      }
  });
    
17.05.2017 / 23:49
-1

Insert the js file into the head tag so you ensure it loads.

<head>
    <title>Minha Página</title>
    <script src="functions-index.js"></script>
</head>

Or if you're using Jquery you can use it.

$.getScript('functions-index.js', function() {

});
    
17.05.2017 / 23:55