12 Aug
Posted by krisgale as compatibility hacks, browsers
i won’t repeat why we must do these things. that was the purpose of the post just prior to this one. i would recommend reading it if you haven’t already.
–
1. selectively use onpropertychange in <select>’s, and add onkeyup
go to: http://krisgale.com/select-fixed.html
this is the source code from the above…
<html><head><title>select</title><script type="text/javascript">function selectchange(){ var frm = document.forms["testform"];var fld = frm["testselect"];var val = fld.options[fld.selectedIndex].value;document.getElementById("testvalue").innerHTML = val;}</script></head><body><form name="testform" action="select.html" method="get"><!--[if IE]><select name="testselect" onpropertychange="selectchange();" onkeyup="selectchange();"><![endif]--><!--[if !IE]>--><select name="testselect" onchange="selectchange();" onkeyup="selectchange();"><!--<![endif]--><option value='1'>a</option><option value='2'>b</option><option value='3'>c</option></select></form><hr /><div id="testvalue"> </div></body></html>this should not work. it should not even be considered valid. but it does and it is.
in essence, what this ponderous pile of poo does, is, attach the same event handler to onkeyup as onchange, to account for when a user uses the keyboard to shortcut to an <option> rather than clicking on it.
and to get around internet explorer’s autocomplete function munging things up even more, that mess of special-purpose html comments, nested inside standard html comments (so the user never sees them), sets up the <select> with an onpropertychange attribute rather than an onchange attribute, in a manner that mutually excludes all browsers but ie 5.0 and later (which is the version that introduced these html comments).
i feel dirty.
2. check for selectedIndex == 0, not a possibly-not-empty empty string
to avoid the pitfall of having an event handler deliver your client-side validation script the contents of an <option> instead of an empty string, you can modify your code to check for selectedIndex == 0 instead.
<select name=”dropdown”>
<option>please select</option>
…
</select>
…
var fld = document.getElementById(”dropdown”);
if(fld.selectedIndex == 0)
{ alert(”please select an option”);
fld.focus();
}
alternatively, you might forego the nicety of the prompt within the dropdown and try an entirely empty first <option>.
<option></option>
but for most, this will be too great a compromise; an initially-blank select box does nothing to direct the user to pick something… on the other hand, if your validation is purely server-side, this may be your only reasonable alternative.
as far as not rewriting code to check for selectedIndex == 0, this may work…
<option value=”>please select</option>
or it may not. it really should, but…
3. use javascript: in href’s and avoid onclick in <a> elements
instead of…
<a id=”link” href=”javascript:void(0);”>click here</a>
…
document.getElementById(”link”).onclick = doSomething;
do this:
<a id=”link” href=”javascript:”>click here</a>
…
document.getElementById(”link”).href=”javascript:doSomething();”;
it’s not nearly as elegant, but at least it will work with ie7.
–
thank you microsoft, for wasting my weekend. i would be grateful if any of these were billable hours, and even then, i’m not sure how much so.
| bookmark it! | ||||||
|
4 Responses
Daniel
August 18th, 2007 at 4:43 pm
1I have to say, that I could not agree with you in 100% regarding o.us poetry, but it’s just my opinion, which could be wrong
krisgale
August 18th, 2007 at 7:21 pm
2talking about this? i’m not entirely sure what it all means.
Matthew Hill
August 30th, 2007 at 8:46 am
3I’ve just come up against this problem with the onclick precedence. Your solution is no good for me because I’m looking for a completely accessible solution that will have the standard fallback behaviour of a correctly working HREF that goes to another page.
Is there really no way around this in IE7? — seems utterly ridiculous!
krisgale
August 30th, 2007 at 9:18 am
4hey matthew,
to get the corrollary (whereby href overrides onclick) in all browsers, this Should work:
<a href=”http://example.com” onclick=”doSomething(); return false;”>click here</a>
though, at times this might actually go to a new blank page that simply says “false” — YMMV.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar
if you want something done right… DIY. is proudly powered by WordPress - BloggingPro theme by: DesignDisease