So I’m new to JS, and I wanna redirect the user to another page…
My code:
// Below function Executes on click of login button. function validate() { redirectTo = window.location.protocol + window.location.host + "/dashboard.html"; var username = document.getElementById("userName").value; var password = document.getElementById("passWord").value; if (username == "admin" && password == "password") { window.location = redirectTo; // Redirecting to other page. return false; } else { alert("NANI!!!"); } }
I know this is not a secure way to auth, but relax it’s just a portfolio project
Advertisement
Answer
You should append the ‘//’ after window.location.protocol
which mentioned by @Vasan.
Using ES6 template strings would make it clear.
function validate() { const redirectTo = `${window.location.protocol}//${window.location.host}/dashboard.html`; const username = document.getElementById('userName').value; const password = document.getElementById('passWord').value; if (username === 'admin' && password === 'password') { window.location = redirectTo; // Redirecting to other page. return false; } else { alert('NANI!!!'); } }