js实现向前
实现页面跳转
window.location.href 可以用于跳转到指定 URL:
window.location.href = 'https://example.com';
window.location.replace 替换当前页面,不保留历史记录:
window.location.replace('https://example.com');
浏览器历史导航
history.back() 返回上一页:
history.back();
history.go() 指定跳转步数,负数表示后退:
history.go(-2); // 后退两页
新窗口打开链接
window.open 在新窗口打开 URL:
window.open('https://example.com', '_blank');
锚点跳转
通过修改 location.hash 跳转页面锚点:
location.hash = '#section1';
表单提交跳转
通过表单提交实现跳转:
<form action="/target-page" method="get">
<button type="submit">跳转</button>
</form>
定时跳转
setTimeout 实现延迟跳转:
setTimeout(() => {
window.location.href = 'https://example.com';
}, 3000); // 3秒后跳转
事件触发跳转
通过点击事件触发跳转:
document.getElementById('btn').addEventListener('click', () => {
window.location.href = 'https://example.com';
});
条件跳转
根据条件判断是否跳转:
if (condition) {
window.location.href = 'https://example.com';
}
相对路径跳转
使用相对路径进行跳转:

window.location.href = '../page.html';






