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

286
Vistas
how to redirect to another page when a certain condition is true using javascript

I'm a newbie at javascript and I'm currently stuck on this problem. I need to redirect to another page based on the number of certain moves. for example: if the number of moves after you find all the pairs are between 4 - 8, you will be redirected to page 1 to see your result, 10 - 16 is on page 2 and so on, but my code regarding of result still goes to page 1. Here is my js code:

 function validate() {
  var clickHere = document.getElementById("terms");
  if(clickHere.checked){
    if(counterVal >= 4 || counterVal <=8){
      location.replace("https://www.w3schools.com/js/")
    }
    else{
      location.replace("https://javascript.info/")
    }
  }else{
    
    alertify.error('Click the checkbox first.')
  }

Ive been trying different method to solve this problem but no luck. Thank you for helping.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The simplest way to use JavaScript to redirect to a URL is to set the location property to a new URL using window.location.href. The JavaScript code looks like this: window.location.href = ‘https://ExampleURL.com/’; it is a property that tells you what URL is currently being viewed. Setting a new value, you are telling the browser to load that new URL, similar to what would happen if a user clicked a link. You function should look like this:

function validate() {
  var clickHere = document.getElementById("terms");
  if(clickHere.checked){
    if(counterVal >= 4 || counterVal <=8){
      window.location.href = "https://www.w3schools.com/js/"
    }
    else{
      window.location.href ="https://javascript.info/"
    }
  }else{
    
    alertify.error('Click the checkbox first.')
  }
about 4 years ago · Santiago Trujillo Denunciar

0

From your question, you have mention for example: if the number of moves after you find all the pairs are between 4 - 8, you will be redirected to page 1 to see your result, 10 - 16 is on page 2 and so on, but my code regarding of result still goes to page 1

As you mentioned the countVal is in between 4 to 8, then it will for this link https://www.w3schools.com/js/

But in this, if the condition you have mentioned as counterVal <=4. The condition will be true if the countVal is more then 8, that is the problem.

if(counterVal >= 4 || counterVal <=8){

You need to rewrite it like this

if(counterVal >= 4 && counterVal <=8){

I hope this would be useful.

about 4 years ago · Santiago Trujillo Denunciar

0

The code need to be

if(counterVal >= 4 && counterVal <=8){
   window.location.href = "https://www.w3schools.com/js/1"
}else if(counterVal >= 10 && counterVal <=16){
   window.location.href = "https://www.w3schools.com/js/2"
}else{
  location.replace("https://javascript.info/")
}
about 4 years ago · Santiago Trujillo Denunciar
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