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

115
Visualizações
JavaScript redirect while passing on the parameter to new URL

Im trying to write the code for a javascript that will redirect to a new url while fetching the url parameter and passing it to the new url.

The visited url = www.example.com/files/?id=12345

New Url to redirect to = www.sample.com/files/?list=12345

This is the below code i have been able to come up with.

<!DOCTYPE html>
<html>
<body>

<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');

window.location.href = `www.sample.com/files/?list=${id}`;

</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

your code works well, but when using window.location.href to redirect, the browser by default asumes that the url you are using is relative to the current site, so if you redirect to "sample.com" you are going to end up in "example.com/sample.com"

So to force the browser to redirect to another site you need to include the full URL with the protocol "https://sample.com"

So the fixed code would be

<!DOCTYPE html>
<html>
<body>

<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');

window.location.href = `https://www.sample.com/files/?list=${id}`;

</script>

</body>
</html>

Note that Im using https, if your site does not have https, change it for http or you are gonna get an error from the browser (and try to get https jeje)

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