0

I'm trying to get part of the URL and redirect to that part.

What I mean is that a Script generates a Link that gets opened. This link would be https://mywebsite.com/?s=https://google.com.

I now want to take the https://google.com part of the URL and redirect to that after a 5 second Timer.

How would I do that?

I tried

<script>
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("s")); 
</script>

Which logs the URL i need in the console but I don't know how to redirect to that URL now after a 5 second Timer

Thanks for your advice!

  • @Dexygen Sorry man, think i searched for the wrong things and didn't fully understand that i could just reuse parsedUrl.searchParams.get("s") in my redirect – Bernhard Alber Aug 11 '22 at 16:07
  • Ok, just know that insufficient research is a reason for down-voting. Just frustrated that other users who should know better didn't vote to close as a duplicate. – Dexygen Aug 11 '22 at 19:09

1 Answers1

0

Good question! You're probably looking for javascript's setTimeout and window.redirect functions. Here is an example of how you use both of them.

const parsedUrl = new URL(window.location.href);

setTimeout(()=>{
    window.redirect(parsedUrl.searchParams.get("s")); // window.redirect takes in a link and redirects a user.
}, 5 * 1000); // setTimeout takes a function and a time in milliseconds
IndevSmiles
  • 737
  • 6
  • 17