Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
How can I modify the content of an HTML page after redirecting from a form using only JavaScript/HTML?

I'm learning HTML and I'm trying to do some very basic stuffs. I have my main page under index.html, a registration form under registration.html, and a script under script.js.

Index.html has the following button: <BUTTON id="login" onclick="register()">login/registration</BUTTON>

function register()
{
    if (!loggedIn)
        document.location.href = "registration.html";
    else
    {
        // Page not created yet
        //document.location.href = "aacount.html";
        console.log("redirecting to account page");
    }

In script.js, at the top, I have let loggedIn = false;.

The form's submission button looks like this: <button type="submit" class="submit" onclick="submit()"><b>Submit</b></button>

function submit(){loggedIn = true;}

Now on my main page, I have the login button. I would like it to change to an Account button so I made this:

function checkLog()
{
    if (loggedIn == true)
        document.getElementById("login").textContent = "Account";
    else
        document.getElementById("login").textContent = "Login/Registration";
}

I want my form, which has the header form class="registration_form" action="index.html" method="post" id="form" to redirect to the main page once you click submit. Since you're now logged in, I want the login/registration to swap to an account button. I don't have any backend because I haven't learned how yet. I just want to modify index.html once it loads back in by using the checkLog() function. Is there a way to do this using only HTML/JavaScript? I have seen a couple solutions using PHP, but I will only jump into it next week once I have more free time. Until then, can I make it work this way?

Just to be sure, I have tried adding onload="checkLog()" to both the entire body text and the login button to no avail. I've also found out that adding onclick="checkLog()" on the submit button in the form doesn't do anything since it executes before the action of changing web pages occur. I basically want a onclick="" that occurs after the action.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As a lot suggested in the comments, since there is no server-side to your project yet you're trying to check if the user is logged in to your website, your best bet is either cookies or LocalStorage. In this answer I will take the LocalStorage approach.

    function register()
{
    if (localStorage.getItem("userAuthentication") === null)
        storage.setItem("userAuthentication", "userAuthentication");
        document.getElementById("login").textContent = "Login/Registration";
    else {
        // User is logged in
        // Since the user must have localStorage set up
        console.log("redirecting to account page");
        document.getElementById("login").textContent = "Account";
    }
}

The getItem attribute returns null if the item does not exist; meaning the item hasn't been set yet. if so, we need to set LocalStorage, and change the login button text to "Login/Registration". Else, the localStorage has to be set, meaning we can can change the login button text to "Account".

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda