March 5, 200917 yr I absolutely love the fact that this works. It allows me a very small amount of browser detection with only a couple of lines of code. My problem is that I don't understand why it works, and I don't like writing JavaScript I don't understand. My functions seem to have a parameter passed to them automatically. Let me show you what I mean. Consider a basic form on a web page: window.onload = function() { var theForms = document.forms; for (var i = 0, i_l = theForms.length; i < i_l; i++) { var theInputs = theForms[i].elements; for (var j = 0, j_l = theInputs.length; i < j_l; j++) { if (window.addEventListener) { document.getElementById(theInputs[j].id).addEventListener('focus', applyClick, false); } else { document.getElementById(theInputs[j].id).attachEvent('onfocus', applyClick); } } } }; We write applyClick to fill the input with a value to demonstrate it works. We define a parameter that the function is looking for, and check against it to change the value we add. function applyClick(evt) { if (evt.target) { var theResult = ""; } else if (evt.srcElement) { theResult = "o_0"; } else { theResult = "!_!"; } document.getElementById("showResult").value = theResult; } Any browser that understands the W3C DOM will now display "" when the input is focused. Microsoft Internet Explorer will display "o_0" and any other browser will display "!_!". However, addEventListener and attachEvent don't allow parameters to be passed to functions. Logically, since "evt" is undefined, theResult should always be "!_!". Why is this not the case? If you would like to see the complete script, I have uploaded it to http://www.sk80.co.uk/random_images/auto.html
March 5, 200917 yr Basically, you're defining a parameter in the event listener to reference the event object. This explains it far better than I ever could.
March 5, 200917 yr Author Basically, you're defining a parameter in the event listener to reference the event object.This explains it far better than I ever could. Wow... I knew there had to be a way for a script to remember how it was called -- I didn't realise I'd stumbled upon it. And so many other things I can do with it too... was the control key pressed? where was it clicked? Thank you for this
Create an account or sign in to comment