[英]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.