JavaScript redirects: how location.assign(), replace(), and href differ

Navigate with location.assign(url), location.replace(url), or location.href = url. After a normal user-triggered navigation, assign() and setting href let Back return to the previous page. replace() replaces the current session history entry.

Choose between assign(), replace(), and href

Method Session history When to use it
location.assign(url) Usually adds an entry Return to the current page later
location.replace(url) Replaces the current entry Skip a temporary page on Back
location.href = url Usually adds an entry Read the address and assign one when needed

Reading location.href gives the current address as a string. Setting it navigates. The link and script below show two ways to open the same destination. A link is enough for routine navigation that a visitor chooses. See HTML Living Standard for the history rules.

<a href="https://example.com/">Open example site</a>
const destination = 'https://example.com/';
window.location.assign(destination);

Match the source and limit which values carry over

To redirect only from a particular address, parse it with new URL(location.href) and compare the origin and path. The origin combines scheme, hostname, and port. This code navigates to a fixed destination only from https://old.example.com/guide/. The paths /guide and /guide/ are distinct here.

const current = new URL(window.location.href);
if (current.origin === 'https://old.example.com' &&
    current.pathname === '/guide/') {
  const destination = new URL('https://new.example.com/guide/');
  const lang = current.searchParams.get('lang');
  if (lang === 'ko' || lang === 'en') {
    destination.searchParams.set('lang', lang);
  }
  window.location.replace(destination.href);
}

The example carries over only an allowed lang value, ko or en. Copy only the parameters you need so unrelated query parameters and fragments are left behind. When deploying, check that the source condition cannot also match the destination.

Complete example: see whether Back skips the source

Save the HTML in UTF-8 as redirect-demo.html and open it in a new tab. Click Open source to put the start and source views in history. Then click the assign() button. From the target, press Back to return to the source.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare browser navigation and history</title>
</head>
<body>
<p id="current"></p>
<div id="home">
  <p>Start view</p>
  <a id="open-source" href="?view=source">Open source</a>
</div>
<div id="source" hidden>
  <p>This is the source view. Choose one of the two buttons.</p>
  <button id="assign" type="button">Navigate with assign()</button>
  <button id="replace" type="button">Navigate with replace()</button>
</div>
<div id="target" hidden>
  <p>This is the target view. The address contains view=target.</p>
  <p>Press Back to see which view opens.</p>
</div>
<script>
const url = new URL(window.location.href);
const source = document.querySelector('#source');
const target = document.querySelector('#target');
const current = document.querySelector('#current');
const view = url.searchParams.get('view');

source.hidden = view !== 'source';
target.hidden = view !== 'target';
document.querySelector('#home').hidden = view === 'source' || view === 'target';
current.textContent = url.href;

function destination() {
  const next = new URL(window.location.href);
  next.searchParams.set('view', 'target');
  return next.href;
}

document.querySelector('#assign').addEventListener('click', () => {
  window.location.assign(destination());
});
document.querySelector('#replace').addEventListener('click', () => {
  window.location.replace(destination());
});
</script>
</body>
</html>

At the source, click the replace() button and press Back again. The target replaces the source entry, so Back skips the source and opens the start view. All three views use the same file with a different view query. Open the file again in a new tab to repeat.

When to use a server redirect or a link

For a URL that has permanently moved, configure the server to send an HTTP redirect with the new URL in its Location header. Consider 301 or 308 for a permanent move and 302 or 307 for a temporary one. Use 307 or 308 when the request method must be preserved. See RFC 9110: Redirection.

Scripted navigation works once the browser loads the original document and runs its code. It fits actions and conditions within a loaded page. For simple navigation to a known URL, use <a href>.