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

124
Visualizações
how to redirect a html page in javascript

I'm working on the Javascript page for my registration form. I simply just can't figure out how to make the page redirect to another page if everything's filled out.

document.getElementById("check").onclick = function() {
  let allAreFilled = true;
  document.getElementById("myForm").querySelectorAll("[required]").forEach(function(i) {
    if (!allAreFilled) return;
    if (!i.value) allAreFilled = false;
    if (i.type === "radio") {
      let radioValueCheck = false;
      document.getElementById("myForm").querySelectorAll(`[name=${i.name}]`).forEach(function(r) {
        if (r.checked) radioValueCheck = true;
      })
      allAreFilled = radioValueCheck;
    }
  })
  if (!allAreFilled) {
    alert('Fill all the fields');
  }
  else {
    window.location.replace("http://www.w3schools.com");
  }
}; 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I'm giving my sample script for the same possible problem I hope this helps

HTML

<pre>
<!DOCTYPE HTML>
<html>
<body>
<p id=P >Fill in all checkboxes</p>
    <form>
        <input type=radio />
        <label>Check 1</label>
        
        <input type=radio />
        <label>Check 2</label>
        
        <input type=radio />
        <label>Check 3</label>
    </form>
</body>
</html>
</pre>

JavaScript

window.onload = () => 
{

var radio = document.querySelectorAll('[type=radio]'),
p = document.getElementById("P");
 
radio.forEach( 
    function(i)
    {
        i.onchange = () => 
        {
            var checked = document.querySelectorAll("[type=radio]:checked"),
                leftover = radio.length - checked.length;
            p.innerHTML = "leftover checkbox " + leftover;
            
            if(radio.length == checked.length)
            {
                alert("All checked");
                window.location.href = "https://ilhamb.com/";
            }
            
        }
    }
)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Use window.location.href to redirect within the same tab /window or use window.open() to redirect in a new tab /window.

As Quentin pointed out, you are already using an appropriate way to redirect. May I suggest opening the console and checking if there are console errors ? Also make sure that your program actually reaches the redirect by using a browser breakpoint or console logs.

about 4 years ago · Juan Pablo Isaza Relatório

0

This code

window.location = "w3schools.com"

Or

window.location.href = "w3schools.com"

should do it

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