-1

I want to create a simeple page on my website that redirects users to another page based on url parameters in javascript. Basically I want to have something like this url https://example.com/go?redirect=google.com that when visited redirects users to google or any url entered in after ?redirect=.

Is this possible in JavaScript, and if so how would I go about writing code to make this function?

sophros
  • 14,672
  • 11
  • 46
  • 75
Zreddx
  • 35
  • 5
  • Maybe this will solve it if someone has the same problem, haven't tried it yet but just found this. https://stackoverflow.com/questions/11823784/redirection-to-a-specific-web-page-based-on-url-parameter-using-javascript – Zreddx Jan 20 '19 at 17:47
  • You could read [query params](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript). – Sebastian Waldbauer Jan 20 '19 at 17:47
  • Thanks for the reply! – Zreddx Jan 20 '19 at 17:54

1 Answers1

0

Try this:

    var url = new URL(window.location.href);
    var navigate = url.searchParams.get("redirect");
    window.location.href=navigate;
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
Sunil
  • 52
  • 9
  • It did work partially. The script knows the parameter and everything, however it redirects to example.com/google.com instead of just google.com. – Zreddx Jan 20 '19 at 18:03