浏览器嗅探错误/不当行为的替代方案(不缺乏功能)
好吧,我们都知道浏览器嗅探是不好的。我不想在这里重新讨论整个辩论,而且 SO 无论如何也不是合适的论坛。我正在寻找一种解决方案来解决所提出的特征检测替代方案似乎无法解决的问题。
在我的特定情况下,它是由如下代码触发的:
<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 绑定一起使用”(除了 提交
,我想)。但这段代码在我们所有“官方”支持的浏览器上都按预期工作,只有当我们有一个用户碰巧在 IE 7 中尝试该网站时,这才成为一个问题(即,它绕过了绑定并提交了表单)老式的方式)。
我们不在 IE 7 上进行测试。因此,我们尝试在首页上使用浏览器检测,并警告其 User-Agent 指示其浏览器不受支持的用户。我不喜欢这样做,但我不清楚替代方案是什么。我想不出一种可以检测“允许单击提交按钮上的绑定来替换实际提交”的功能检测策略。这里最好的方法是什么?
编辑澄清:我并不是要求上面代码的替代方案。我知道它们是什么。我问我应该如何检测上述代码有问题,以便它不会达到生产,而不必在 IE 7 中实际测试它。
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;
});
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).
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
劫持默认按钮事件?
Hijack the default button event?