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

249
Visualizações
Conditional redirection depending on Current URL. through html

(1) My example Current URL along with Parameters is ---- www.example.com?fname=John&femail=john123@example.com

(2) Through html / JavaScript I want to check Current URL Parameter whether it contains any data in fname

(3a) Next, If there is No URL Parameter present then Redirect to "www.example.com/error-page" or (3b) If the parameter fname have some data (No need for any Validation of data) meaning the parameter fname is not empty then should Continue with the execution of Current Page.

I tried the following successfully :

<!doctype html> <html> <head> <body> <div> <p id ="dd"></p> </div> <meta charset="UTF-8"/> <script type="text/javascript"> var iid=document.getElementById("dd"); var getURL=window.location.href; var theString = window.location.href; var theWord = "fname"; var theWordExp = /fname/g; if (theString.search(theWordExp) == -1) { window.location.href= ('www.example.com/error-page'); }; </script> </body> </head> </html>

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

0

Explanation:

  • "I want to check Current URL Parameter whether it contains any data in fname"

The getQueryParam function is explained here How to get "GET" request parameters in JavaScript?

basically it's almost the same as your approach using the location href to parse the params

  • "If there is No URL Parameter present then Redirect to" else continue, for this you'll only need to wrap it inside a div, if the conditional is false (found param) then it'll just not run the statement inside if block (the one that will redirect user to error page)

Note that you have many other option to implement, check with the compatibility of browser, behaviour of redirection can also be changed to replace the last history page so user cannot go back to the previous URL that throw the error using window.location.replace method

const getQueryParam = (name) => {
  if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search))
    return decodeURIComponent(name[1]);
}

let fnameParam = getQueryParam("fname");
if (!fnameParam) {
  window.location = "http://www.example.com/error-page";
};
<!doctype html>
<html>

<head>

  <body>
    <div>
      <p id="dd"></p>
    </div>
  </body>
</head>

</html>

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