问题描述
好吧,我们都知道浏览器嗅探是不好的.我不想在这里重新讨论整个辩论,因此无论如何都不是合适的论坛.我正在寻找一种解决方案,以解决提议的特征检测替代方案似乎无法解决的问题.
Ok we all know that browser sniffing is bad. I'm not looking to rehash the entire debate here, and SO isn't the right forum for that anyway. I'm looking for a solution to a problem that the proposed alternative of feature-detection doesn't seem to address.
在我的特定情况下,它是由以下代码触发的:
In my particular case it was triggered by code like the following:
<form name="something" method="POST">
<input name="input1"><input name="input2"> <!-- etc -->
<input id="mysubmit" type="submit" value="Submit me">
</form>
<!-- during jquery initialization -->
$('#mysubmit').click(function() {
sendAjaxRequestInsteadOfRealForm($('#input1').val(), $('#input2').val());
return false;
});
现在,我知道真正的答案是不要将input type="submit"
与jQuery绑定一起使用"(我想不是submit
).但是此代码在我们所有官方"支持的浏览器上均按预期工作,并且只有当我们有一个用户恰巧尝试在IE 7中尝试该网站时,这才成为问题(即,它绕过了绑定并提交了表单老式的方式).
Now, I know that the real answer here is "don't use input type="submit"
with jQuery bindings" (other than submit
, I suppose). But this code worked as expected on all of our "officially" supported browsers, and it was only when we had a user who happened to try the site in IE 7 that this became a problem (namely, it bypassed the binding and submitted the form the old-fashioned way).
我们未在IE 7上进行测试.因此,我们尝试在首页上使用浏览器检测,并警告其User-Agent指示其浏览器不受支持的用户.我不喜欢这样做,但不清楚替代方法是什么.我无法想到一种功能检测策略,该策略可以检测允许单击提交按钮上的绑定来替换实际的提交".最好的方法是什么?
We don't test on IE 7. So we try to use browser detection on our front page and warn users whose User-Agent indicates their browser is unsupported. I don't like doing this, but I'm unclear on what the alternative is. I can't think of a feature-detection strategy that would detect "allows click bindings on submit buttons to replace actual submission". What's the best approach here?
编辑进行澄清:我不是在寻找替代上面代码的方法.我知道他们是什么.我在问如何将上述代码检测为有问题的代码,以便不必在IE 7中进行实际测试就可以将其投入生产.
EDIT for clarification: I'm not asking for alternatives to the code above. I know what they are. I'm asking how I should detect the above code as problematic so that it would not have reached production, without having to actually test it in IE 7.
推荐答案
劫持默认按钮事件?
$('#mysubmit').click(function() {
event.preventDefault(); // don't let the click event happen
sendAjaxRequestInsteadOfRealForm($('#input1').val(), $('#input2').val());
return false;
});
这篇关于错误/行为异常的浏览器嗅探替代品(不缺少功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!