Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
JavaScript code that's supposed to return the user to a login page if a cookie doesn't exist, doesn't work

So I have multiple pages connected that should check if a cookie exists when first visited, as the cookie wont exist it should send the person to the login page where after put in their username, password and time limit for the cookie, the script creates a cookie and redirects them back to the index page. Now everything works besides the redirecting part, because I'm guessing its not fetching the cookie right so it keeps sending you to the login page as soon as you get redirected to where you were supposed to.

function upisi_cookie() {
var danas = new Date();
var istice = new Date();
var username = document.prijava.username.value;
var password = document.prijava.password.value;
var trajanje = parseInt(document.prijava.trajanje.value);

istice.setTime(danas.getTime() + (trajanje * 1000));
let expires = "expires=" + istice.toUTCString();
document.cookie = "User=" + username + ";" + expires + ";path=/";}

Above is the cookie creation code. And this is for fetching the cookie and checking weather it exists or not (and redirection if it does exist)

function getCookie(User) {
    var dc = document.cookie;
    var prefix = User + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
    }
    return decodeURI(dc.substring(begin + prefix.length, end));
}
window.onload = function ControlCookies() {
    var myCookie = getCookie(User);
    stranicaCurrent = document.getElementsByTagName("title");
    if (myCookie == null && stranicaCurrent != "prijava") {
        window.location.href = "---login page location---"; //sends the user to a login page if there are no cookies
    }
    else {
    }
}

 
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda