防止意外返回浏览

发布于 2024-09-16 12:48:08 字数 168 浏览 6 评论 0原文

我有一位客户刚刚购买了 Apple Magic Mouse。我们建立了一个菜谱网站,当她输入菜谱的所有信息时,她感到非常沮丧,然后拿起她的新鼠标,不小心滚动到右侧,它会进行后退浏览,她的所有信息都丢失了。所以她想警告/弹出以防止该页面上发生这种情况。

有什么建议或者给我指出一些可以做到这一点的 JS 吗?

I have a client that just got the Apple Magic Mouse. We built a recipe website and she get very upset when she is inputting all the information for the recipe, then goes for her new mouse and accidentally scrolls to the right and it will do a Back Browse and all her info is lost. So she wants to warning/pop up to prevent this from happening on that page.

Any suggestions or point me to some JS that does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-09-23 12:48:08

这是一个基本的“离开前警告”脚本。当用户按下文本区域中的按键时,如果用户要离开页面,系统会通知用户:

<script type="text/javascript">
var changes = false;
window.onbeforeunload = function(){
   if(changes){
      return "You're about to leave this page.";
   }
};
</script>
<textarea onkeypress="window.changes=true">recipe here</textarea>

或者,您可以在页面加载时将文本字段的内容存储在变量中,然后进行比较。当访问者只需按文本区域中的箭头键而不实际修改它时,这将消除一些混乱。

This is a basic 'warn before leave' script. When the user presses a key in the textarea, the user will be notified if (s)he is about to leave the page:

<script type="text/javascript">
var changes = false;
window.onbeforeunload = function(){
   if(changes){
      return "You're about to leave this page.";
   }
};
</script>
<textarea onkeypress="window.changes=true">recipe here</textarea>

Alternatively, you can store the contents of the textfield in a variable when the page loads and compare it afterwards. This will take away some confusion when the visitor just press the arrow keys in the textarea without actually modifying it.

夏末 2024-09-23 12:48:08

所以她想要警告/弹出窗口以防止该页面上发生这种情况。

您可以连接到 浏览器的 onBeforeUnload 事件,然后显示一条确认消息。

So she wants to warning/pop up to prevent this from happening on that page.

You can hook into the onBeforeUnload event of your browser and then show a confirmation message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文