JavaScript のページ遷移とリダイレクト:location.assign()・replace()・href の違い

ページを移動するには location.assign(url)、location.replace(url)、または location.href = url を使用します。通常のクリックによる移動では assign() と href の代入で前のページに戻れます。replace() は現在の履歴項目を置き換えます。

assign()、replace()、href を使い分ける

方法 セッション履歴 適した用途
location.assign(url) 通常は項目を追加 現在のページへ後で戻る
location.replace(url) 現在の項目を置き換える 戻るときに中間画面を飛ばす
location.href = url 通常は項目を追加 アドレスを読み、必要に応じて代入する

location.href を読むと現在のアドレスを文字列として取得できます。値を代入すると移動します。下のリンクとスクリプトは同じ行き先を開く方法です。閲覧者が選ぶ通常の移動ならリンクで十分です。履歴の規則は HTML Living Standard を参照してください。

<a href="https://example.com/">サンプルサイトを開く</a>
const destination = 'https://example.com/';
window.location.assign(destination);

出発アドレスと引き継ぐ値を限定する

特定のアドレスからだけ移動させるには new URL(location.href) で解析し、オリジンとパスを比較します。オリジンはスキーム、ホスト名、ポートの組み合わせです。このコードは https://old.example.com/guide/ からのみ固定の行き先へ移動します。ここでは /guide と /guide/ は別のパスです。

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);
}

許可した lang の値 ko または en だけを引き継ぎます。必要なパラメーターだけ指定することで他のクエリパラメーターやフラグメントが移動先に引き継がれません。公開前に、移動先で出発条件が再び成立しないことを確認します。

完全なサンプル:戻る操作で出発画面を飛ばすか確認

HTML を UTF-8 で redirect-demo.html として保存し、新しいタブで開きます。「出発画面を開く」で開始画面の次に出発画面が履歴に入ります。assign() ボタンを押し、到着画面から戻る操作で出発画面に戻ります。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ブラウザーのページ遷移と履歴を比較</title>
</head>
<body>
<p id="current"></p>
<div id="home">
  <p>開始画面</p>
  <a id="open-source" href="?view=source">出発画面を開く</a>
</div>
<div id="source" hidden>
  <p>出発画面です。どちらかのボタンを押してください。</p>
  <button id="assign" type="button">assign() で移動</button>
  <button id="replace" type="button">replace() で移動</button>
</div>
<div id="target" hidden>
  <p>到着画面です。アドレスに view=target があります。</p>
  <p>戻る操作でどの画面が開くか確認してください。</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>

出発画面で replace() を押し、もう一度戻ります。到着画面が出発画面の履歴項目を置き換えるので、開始画面が開きます。三つの画面は、同じファイルで view クエリの値を変えたものです。やり直すには新しいタブでファイルを開きます。

サーバーのリダイレクトやリンクが適する場合

URL が恒久的に変わった場合は、サーバーから新しい URL を Location ヘッダーに入れた HTTP リダイレクトを返します。恒久的な移転は 301 または 308、一時的な移動は 302 または 307 を検討します。リクエストメソッドを保つ必要があれば 307 または 308 を使います。詳しくは RFC 9110: Redirection を参照してください。

スクリプトによる移動は、ブラウザーが元の文書を受け取ってコードを実行してから起きます。開いているページの操作や条件分岐に適しています。既知の行き先に普通に移動する場合は <a href> を使います。