JavaScript 跳转 HTML 页面方法:直接跳转:window.location.href = "new_page.html";相对路径跳转:window.location.href += "/new_page.html";使用 pushState() 跳转:window.history.pushState("", "", "new_page.html");
如何使用 JavaScript 跳转 HTML 页面
直接跳转:
window.location.href = "new_page.html";
相对路径跳转:
window.location.href += "/new_page.html";
使用 pushState() 跳转:
立即学习“前端免费学习笔记(深入)”;
window.history.pushState("", "", "new_page.html");
详细信息:
直接跳转:
直接使用 window.location.href 可以直接跳转到指定 URL。这是最简单的方法,但要注意的是它会加载一个新的完整页面。
相对路径跳转:
相对路径跳转可以相对当前页面跳转到指定 URL。例如,如果您当前在 index.html 页面,并且想跳转到 about.html 页面,可以使用:
window.location.href += "/about.html";
使用 pushState() 跳转:
立即学习“前端免费学习笔记(深入)”;
pushState() 方法允许您在不加载新页面的情况下更改 URL。这在需要更新页面而无需重新加载时很有用。
window.history.pushState("", "", "new_page.html");
此方法将 URL 更改为 "new_page.html",但不会加载新页面。如果需要恢复到原始页面,可以使用 window.history.back()。
以上就是html如何跳转到js的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号