问题
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.
回答1:
Hijack the default button event?
$('#mysubmit').click(function() {
event.preventDefault(); // don't let the click event happen
sendAjaxRequestInsteadOfRealForm($('#input1').val(), $('#input2').val());
return false;
});
来源:https://stackoverflow.com/questions/7718673/browser-sniffing-alternative-for-bugs-misbehaviors-not-lack-of-features