实现js页面跳转页面跳转页面
使用 window.location.href 实现跳转
通过修改 window.location.href 属性,可以直接跳转到目标 URL。这种方法会触发页面刷新,并在浏览器历史记录中留下记录。
window.location.href = "https://example.com";
使用 window.location.replace 实现跳转
window.location.replace 方法会跳转到新页面,但不会在浏览器历史记录中留下当前页面的记录。适用于不希望用户返回原页面的场景。
window.location.replace("https://example.com");
使用 window.open 实现新窗口跳转
window.open 方法可以在新窗口或标签页中打开目标 URL。通过参数可以控制窗口行为(如尺寸、工具栏等)。
window.open("https://example.com", "_blank");
使用 meta 标签实现自动跳转
在 HTML 的 <head> 中添加 meta 标签,可以设置页面在指定时间后自动跳转。适用于简单的重定向需求。
<meta http-equiv="refresh" content="5;url=https://example.com">
使用 history.pushState 实现无刷新跳转
history.pushState 可以修改浏览器地址栏的 URL 而不刷新页面,通常用于单页应用(SPA)。需配合事件监听实现内容更新。
history.pushState({}, "", "new-page.html");
使用表单提交实现跳转
通过动态创建表单并提交,可以模拟页面跳转。适用于需要传递复杂数据或 POST 请求的场景。
const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();






