直接返回上一页的方法有以下几种:使用浏览器内置的 history.back() 方法:history.back();使用 window.location 对象:window.location.href = document.referrer;创建自定义按钮,并使用 window.history.back() 方法返回上一页。

如何使用 JavaScript 返回上一页
直接返回上一页
最简单的方法是使用浏览器内置的 history.back() 方法:
<code class="js">history.back();</code>
使用 window.location
你还可以使用 window.location 对象返回上一页:
<code class="js">window.location.href = document.referrer;</code>
其中,document.referrer 包含了上一个页面的 URL。
使用自定义按钮
如果你不想使用浏览器默认的行为,可以创建自己的按钮并使用以下代码来返回上一页:
<code class="html"><button onclick="goBack()">返回上一页</button></code>
<code class="js">function goBack() {
window.history.back();
}</code>注意事项
history.back() 方法不会触发 onbeforeunload 事件,因此无法执行弹窗确认等操作。history.back() 方法将无效。history.back() 方法。以上就是js怎么返回上一页的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号