Skip to main content

Redirects to a specified URL.

/**
 * Redirects to a specified URL.
 *
 * https://www.30secondsofcode.org/js/s/redirect
 *
 * - Use Window.location.href or Window.location.replace() to redirect to url.
 * - Pass a second argument to simulate a link click (true - default) or an HTTP redirect (false).
 *
 * @param {String} url The URK to redirect.
 * @param {Boolean} asLink Whether or not to to simulate a link click (true - default), or an HTTP redirect (false).
 */
const redirect = (url, asLink = true) =>
  asLink ? (window.location.href = url) : window.location.replace(url);

// Example
redirect("https://google.com");

// -----------------------------------------------------
// ES5 version
// -----------------------------------------------------

var redirect = function (url, asLink) {
  if (asLink === void 0) {
    asLink = true;
  }
  return asLink ? (window.location.href = url) : window.location.replace(url);
};

// Example
redirect("https://google.com");