简体   繁体   English

当用户尝试按“后退”按钮或使用Firefox时,如何添加警报? (JavaScript)

[英]How do I add an alert when the user tries to press the back button or is using firefox? (Javascript)

只需使用一些Javascript来警告按下后退按钮的人们可能会使用“确定/取消”按钮来破坏他们的交易,还需要一种检测他们是否正在使用任何版本的firefox的方式。

You could use something like http://www.sajithmr.me/warning-before-navigate-away-from-a-page/ in order to check if a user is about to leave the page. 您可以使用类似http://www.sajithmr.me/warning-before-navigate-away-from-a-page/之类的东西来检查用户是否要离开该页面。 I'm not sure if there is a solution to target the back-button specifically. 我不确定是否有专门针对后退按钮的解决方案。

As to your browser detection question, I don't really like using user agent based browser detection, its better to do feature testing onLoad. 至于您的浏览器检测问题,我不太喜欢使用基于用户代理的浏览器检测,最好在onLoad上进行功能测试。 But if you are just looking to see if someone is using (or says they are using) Firefox, you can parse the useragent string. 但是,如果您只是想查看是否有人正在使用Firefox(或说他们正在使用),则可以解析useragent字符串。 Here is an example of this from the jQuery 1.3.2 source: 这是来自jQuery 1.3.2源的示例:

var userAgent = navigator.userAgent.toLowerCase();

// Figure out what browser is being used
jQuery.browser = {
    version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
    safari: /webkit/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

This SO question: Detecting Back Button/Hash Change in URL will answer most of your question. 这样的问题: 检测URL中的“后退按钮/哈希”更改将回答您的大多数问题。

Detecting the back button is a tricky business because it's the same behaviour as closing the window. 检测返回按钮是一项棘手的工作,因为它与关闭窗口的行为相同。

You can use the onbeforeunload event handler to handle this but like i say, it's a bit tricky. 您可以使用onbeforeunload事件处理程序来处理此问题,但是就像我说的那样,这有点棘手。

Both back-breaking and UA-sniffing are a huge code smell. 回传和UA嗅探都是巨大的代码味道。

A properly-coded web application should not “screw up” when the user goes back a step in a transaction. 当用户退回事务中的步骤时,正确编码的Web应用程序不应“增加”。 If it does, you probably have a collection of concurrency problems which will make your site unpleasant to use. 如果是这样,您可能会遇到一系列并发问题,这些问题将使您的网站无法使用。

UA-sniffing is highly troublesome, prone to false positives and negatives and, if done on the server end, proxy problems. UA嗅探非常麻烦,容易出现误报和否定的问题,如果在服务器端进行,也会导致代理问题。 What is the reason you want to detect Firefox? 您想检测Firefox的原因是什么? If you are trying to work around a particular Firefox problem there is almost certainly a better approach focusing on that one feature rather than trying to detect a particular browser. 如果您要解决特定的Firefox问题,几乎可以肯定有一种更好的方法专注于该功能,而不是尝试检测特定的浏览器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中,最好是JQuery,当用户点击浏览器中的“后退”按钮时,如何在URL中添加内容? - In Javascript, preferably JQuery, how do I add something to the URL when the user clicks “back” button in the browser? 如何仅在用户按下 Enter 按钮时进行搜索? - How do I search only when user press enter button? 当用户按下“后退”按钮时,使用“历史记录”对象/ JavaScript将用户重定向到特定锚点吗? - Using History object/javascript to redirect the user to a specific anchor when they press the back button? 如何在此JavaScript和HTML代码中添加按钮,以便在按下按钮时,交通信号灯序列运行一次? - How do i add a button to this JavaScript and HTML code so that when i press the button, my traffic light sequence runs once? 有一个带有链接Java脚本的HTML文档,当我单击提交按钮时,我试图添加一条警报消息。 我该怎么做呢? - have an HTML document with a linked Javascript I am trying to add an alert message when a submit button is clicked. How do I do this? 使用 javascript 尝试 3 次后如何禁用按钮? - How do I disable a button after 3 tries with javascript? 当我在浏览器上按回时如何重置单选按钮 - How do I reset a radio button when I press back on the browser 如何提醒用户是否尝试单击禁用的单选按钮 - How to alert if user tries to click a disabled radio button Sweet Alert 2 和 javascript:如何制作一个甜蜜警报按钮,点击后将我发送到某个网页 - Sweet Alert 2 and javascript:How do i make a Sweet alert button that sends me to a certain webpage when clicked 使用Firefox“后退”按钮时不会执行启动JavaScript代码 - Startup JavaScript code is not executed when using the Firefox “Back” button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM