CHOO YJ
CHOO YJ

Reputation: 171

How to redirect parameter to a url?

How to redirect a parameter like http://example.com/?link=https://google.com to https://google.com?

Upvotes: 0

Views: 75

Answers (1)

rafaelcastrocouto
rafaelcastrocouto

Reputation: 12161

Very simple way of achieving this redirect with Javascript

function isValidURL(string) {
  var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
  return (res !== null)
};

var urlParams = new URLSearchParams(location.search);
var redirectLink = urlParams.get('link');
if (isValidURL(redirectLink)) {
  window.location = redirectLink;
}

isValidURL function author: vikasdeep-singh

Upvotes: 1

Related Questions