Document.locattion.href does not work in javascript

Question:

The idea is that, on a page that contains a form, it is redirected from the current page to the main page when it is filled out. The problem is that it stays on the same page. The javaScript code is as follows:

function crearSesion(){
var user=document.getElementById("usuario").value;
var pass=document.getElementById("clave").value;
  if(user!==""&& pass!==""){
    if(user=='julio_godoy_mu@hotmail.com' && pass=='javascript'){
       sessionStorage.setItem("user2",pass);
       alert("ingresado correctamente");
       document.location.href="index.html";
    }else{
        alert("no estas registrado");

    }
   }
}

The call to the function from HTML5 code is as follows:

     <input onclick="crearSesion();"type="submit" value="iniciar sesion" id="enviar">

From already very grateful.

Answer:

The button does a submit and I think you don't want it to. Therefore you must change the type and pass it to button

<input onclick="crearSesion();"type="button" value="iniciar sesion" id="enviar">
Scroll to Top