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="zh">
<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 已永久迁移,可让服务器在 HTTP 重定向响应的 Location 标头中提供新地址。永久迁移可考虑 301 或 308,临时迁移可考虑 302 或 307。需要保留请求方法时,请使用 307 或 308。参见 RFC 9110: Redirection。
脚本跳转需要浏览器先加载原始文档并执行代码,适合页面加载后的操作和条件判断。对于固定地址的普通导航,可使用 <a href>。




