通过js实现页面的跳转
使用 window.location.href
通过修改 window.location.href 属性实现页面跳转。这种方法会记录跳转历史,用户可以通过浏览器的返回按钮返回到上一个页面。
window.location.href = "https://example.com";
使用 window.location.replace
window.location.replace 方法会替换当前页面,不会在浏览历史中留下记录,用户无法通过返回按钮回到原页面。
window.location.replace("https://example.com");
使用 window.open
window.open 方法可以在新窗口或标签页中打开页面。可以通过参数控制是否在新窗口打开。
window.open("https://example.com", "_blank");
使用 meta 标签自动跳转
在 HTML 的 <head> 部分插入 meta 标签,设置自动跳转的时间和目标 URL。
<meta http-equiv="refresh" content="5;url=https://example.com">
使用表单提交跳转
通过动态创建表单并提交,实现页面跳转。适用于需要传递表单数据的场景。
const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用 history.pushState 或 replaceState
history.pushState 和 history.replaceState 可以修改浏览器历史记录而不刷新页面,适用于单页应用(SPA)。
history.pushState({}, "", "https://example.com");
使用锚点跳转
通过修改 URL 的哈希部分实现页面内跳转,适用于单页应用中的锚点导航。
window.location.hash = "#section1";






