浏览器嗅探错误/不当行为的替代方案(不缺乏功能)

发布于 2024-12-08 21:01:31 字数 1064 浏览 0 评论 0原文

好吧,我们都知道浏览器嗅探是不好的。我不想在这里重新讨论整个辩论,而且 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 技术交流群。

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

发布评论

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

评论(1

绝影如岚 2024-12-15 21:01:31

劫持默认按钮事件?

$('#mysubmit').click(function() {
    event.preventDefault();  // don't let the click event happen
    sendAjaxRequestInsteadOfRealForm($('#input1').val(), $('#input2').val());
    return false;
});     

Hijack the default button event?

$('#mysubmit').click(function() {
    event.preventDefault();  // don't let the click event happen
    sendAjaxRequestInsteadOfRealForm($('#input1').val(), $('#input2').val());
    return false;
});     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文