How to redirect to a url using url parameters

Asked

Viewed 499 times

2

I would like to get a url that is found right after red= and redirect to it in case it is blank not redirect. I used window.location.replace("") only to test the code.
Example of where I’m going with this: red=http://www.google.com/
The code has the function of replacing the content within an element, and redirect the page in a few seconds.

window.onload = function substituir() {

   var url = new URL(window.location);
   var sub = url.searchParams.get("sub");
   var red = url.searchParams.get("red");
   setTimeout(redirecionar, 5000);

   if(sub == 1) {
       document.getElementById("subst").innerHTML = "<p>Alguma coisa</p>"
   }

   function redirecionar() {

      if(red == 1) {
         window.location.replace("https://www.google.com");
      }
   }
}

2 answers

2


Just check if the variable has value with if(variável) and redirect with window.location.href.

The value in sub will also be sent to the element if it is not undefined and is not null.

window.onload = function substituir() {

   var url = new URL(window.location);
   var sub = url.searchParams.get("sub");
   var red = url.searchParams.get("red");
   setTimeout(redirecionar, 5000);

   if(sub) {
       document.getElementById("subst").innerHTML = "<p>"+sub+"</p>"
   }

   function redirecionar() {

      if(red) {
         window.location.href = red;
      }
   }
}

If the http:// is missing you can check using a regex with .test() and concatenate to the value of red:

if(red) {
   if(!/^http:\/\//.test(red)){
      red = "http://"+red;
   }
   window.location.href = red;
}

A regex /^http:\/\// checks if at the beginning of the string it has http://.

  • Interesting to send the value in sub for the element, but the redirect problem if the url does not contain http, would be in the way of ftp or a magnetic url for example

  • All right I’ll try to find a solution, thank you.

  • It would be more feasible if the redirect worked with any type of url examples: exemplo.com www.exemplo.com http://www.exemplo.com ftp:// magnet:

  • Yes, but how do I know what the link protocol is? If it is http, ftp etc

  • Is it possible to redirect it to anything? example: red=algumacoisa without it redirecting as a site page, thus ignoring everything that comes before: http://www.exemplo.com/?sub=1&red= algumcoisa

  • You can redirect it wherever you want, as long as you set it to where. Now, "anything" doesn’t exist. Imagine you arrive in a pharmacy to buy and say: "give me something"... it doesn’t make sense.

  • yes I imagined that it was not possible, just wanted to remove the doubt really, but anyway, it would be interesting if there is ftp:// magnet: did not apply the regex /^http:\/\//

Show 3 more comments

1

Code

 $().ready(function () {

    var url = new URL(window.location);
    var sub = url.searchParams.get("sub");
    var red = url.searchParams.get("red");
    setTimeout(redirecionar, 5000);

    if(sub == 1) {
       document.getElementById("subst").innerHTML = "<p>Alguma coisa</p>"
    }

    function redirecionar() {
      if (red.length > 0)
        window.location.replace(red);
    }

  })

Explanation

The difference to your code is that I changed the condition for redirecting the page. In the new condition it is checked whether the size of the string captured by the parameter red is greater than 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.