js实现刷新
使用 location.reload()
通过调用 location.reload() 方法可以实现页面刷新。默认情况下会从浏览器缓存加载页面,若需要强制从服务器重新加载,可传入参数 true。
// 从缓存刷新
location.reload();
// 强制从服务器刷新
location.reload(true);
使用 location.replace()
通过 location.replace() 可以刷新当前页面,但不会在浏览器历史记录中生成新记录。适合需要避免用户回退到旧页面的场景。

location.replace(location.href);
使用 location.href 重定向
通过重新赋值 location.href 实现刷新,原理与用户手动输入 URL 后回车相同。
location.href = location.href;
使用 history.go(0)
利用 History API 的 go() 方法,参数 0 表示刷新当前页面。

history.go(0);
使用 meta 标签自动刷新
在 HTML 的 <head> 中添加 meta 标签可实现定时自动刷新,时间单位为秒。
<meta http-equiv="refresh" content="5"> <!-- 每5秒刷新一次 -->
使用 fetch 实现无感刷新
结合 Fetch API 先获取最新数据,再通过 DOM 操作更新页面内容,实现无刷新更新。
fetch(window.location.href)
.then(response => response.text())
.then(html => {
document.open();
document.write(html);
document.close();
});
注意事项
- 强制刷新(
reload(true))可能导致服务器负载增加。 - 高频刷新可能被浏览器拦截或触发安全策略。
- 单页应用(SPA)建议使用路由或状态管理工具实现局部更新而非全局刷新。






