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

227
Visualizações
How to identify the browser and redirect the user if that's not the correct browser?

I have a website to do for university and the teacher wants us to include code that checks what browser the user is using and if the browser isn't Firefox, they should be redirected elsewhere.

I have this so far:


function fnBrowserDetect(){                            
  let userAgent = navigator.userAgent;          
  let browserName;                    
  if(userAgent.match(/chrome|chromium|crios/i)){              
    browserName = "chrome";            
  }else if(userAgent.match(/firefox|fxios/i)){              
    browserName = "firefox";            
  }  else if(userAgent.match(/safari/i)){
    browserName = "safari";            
  }else if(userAgent.match(/opr\//i)){              
    browserName = "opera";            
  } else if(userAgent.match(/edg/i)){              
    browserName = "edge";            
  }else{              
    browserName="No browser detection";            
  }                     
document.querySelector("h1").innerText="You are using "+ browserName +" browser";            
}

function redirect(){
  if(browserName != "firefox"){
    window.location.replace("www.google.com");
  } else {
    window.location.href = "../index.php";
  }
}

Can someone help me out and let me know how can I do this correctly?

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

0

At the end of fnBrowserDetect, you can return browserName so you can use it in your other function.

Also make sure to include the protocol (https://) in your redirect URL.

function fnBrowserDetect(){                            
  let userAgent = navigator.userAgent;          
  let browserName;                    
  if(userAgent.match(/chrome|chromium|crios/i)){              
    browserName = "chrome";            
  }else if(userAgent.match(/firefox|fxios/i)){              
    browserName = "firefox";            
  }  else if(userAgent.match(/safari/i)){
    browserName = "safari";            
  }else if(userAgent.match(/opr\//i)){              
    browserName = "opera";            
  } else if(userAgent.match(/edg/i)){              
    browserName = "edge";            
  }else{              
    browserName="No browser detection";            
  }                     
  document.querySelector("h1").innerText="You are using "+ browserName +" browser";         
  return browserName;
}

function redirect(){
  if(fnBrowserDetect() != "firefox"){
    window.location.replace("https://www.google.com");
  } else {
    window.location.href = "../index.php";
  }
}
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