[WSG] addEventListener
Hi,I'm not sure if this is a right forum to ask this question, please tell me if it isn't...Anyways, what I need to do is to add a onclick event handler to some links, and make this return false. My goal is that users with _javascript_ turned on would go somewhere else than users without. Now, I need the click event to return false, so that _javascript_ users will not folloq the link in the markup. But, for some reason a click event added with addEventListener doesn't seem to ever return false. Does anybody know how to do it? AttachEvent works fine (for IE). I can achieve this using element.onclick, I'd just like to use addEventListener for the sake of standards...For example I could have markup:ul id=listlia href="" nojavascript.htmlLink/a/lilia href="" 2/a/lilia href="" 3/a/li /uland _javascript_:function changeLinks(){ var list = document.getElementById(list).childNodes; for(var i=0;ilist.length;i++){ if(list[i].childNodes.length0){ var a = list[i].firstChild; addEvent(a,click,function(){alert('not going there!');return false;}); } }};function addEvent(obj,evnt,func){ if ( obj.addEventListener){ obj.addEventListener(evnt,func,true); } else if (o.attachEvent){ return o.attachEvent(on+evnt,func); }}When clicked any of the links, the above example should in _javascript_-enabled clients display an alert not going there and NOT follow the link... but it doesn't work in either FireFox or Opera 8 (on windows XP).
Re: [WSG] addEventListener
Use the onclick event; a.onclick = function() { alert('not going there!');return false; } Its not an issue of standards it's in the javascript not the html. Richard -- DonkeyMagic: Website design development http://www.donkeymagic.co.uk ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help **
Re: [WSG] addEventListener
On 1/19/06, Richard Stephenson [EMAIL PROTECTED] wrote: Use the onclick event; a.onclick = function() { alert('not going there!');return false; } Its not an issue of standards it's in the javascript not the html. Richard http://webstandardsgroup.org/mail/guidelines.cfm # Implementing Web Standards - eg: technologies such as HTML, XHTML, CSS, DOM, UAAG, RDF, XML, JavaScript and EcmaScript # Discussing best practice in these technologies It is an issue of standards, actually. And best practice in terms of using unobtrusive JS + event listeners. Josh ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help **
Re: [WSG] addEventListener
.onclick works as I said before... I was just under impression that onclick is not a preferred way of setting event handlers.Also, I wrote this also as I think that AddEventHandler SHOULD work? Shoudln't it? On 1/19/06, Joshua Street [EMAIL PROTECTED] wrote: On 1/19/06, Richard Stephenson [EMAIL PROTECTED] wrote: Use the onclick event; a. { alert('not going there!');return false; } Its not an issue of standards it's in the _javascript_ not the html. Richardhttp://webstandardsgroup.org/mail/guidelines.cfm # Implementing Web Standards - eg: technologies such as HTML, XHTML,CSS, DOM, UAAG, RDF, XML, _javascript_ and EcmaScript# Discussing best practice in these technologiesIt is an issue of standards, actually. And best practice in terms of using unobtrusive JS + event listeners.Josh**The discussion list forhttp://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help**
Re: [WSG] addEventListener
Fair enough. Being a bit hast there wasn't i. However the support of the DOM2 Event handlers standards is still incomplete so as far i know and Mozilla apperas to have a bug with the return value. That may be your problem. http://www.gerd-riesselmann.net/archives/2005/03/a-firefox-javascript-bug -- DonkeyMagic: Website design development http://www.donkeymagic.co.uk ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help **
Re: [WSG] addEventListener
Miika, Today is my quirksmode day ;-) http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html http://www.quirksmode.org/blog/archives/coding_techniques/memory_leaks/index.html I tend to use .onclick for attaching events to links, because it has fewer cross browser issues than the modern event listeners. But I prefer to use event listeners for events like window.onload. regards Martin ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help **
Re: [WSG] addEventListener
Thanks a lot!That's not exactly a bug, it's how it's supposed to work:http://www.gerd-riesselmann.net/archives/2005/04/firefox-canceling-problem-solved On 1/19/06, Richard Stephenson [EMAIL PROTECTED] wrote: Fair enough. Being a bit hast there wasn't i. However the support ofthe DOM2 Event handlers standards is still incomplete so as far i knowand Mozilla apperas to have a bug with the return value. That may beyour problem. http://www.gerd-riesselmann.net/archives/2005/03/a-firefox-_javascript_-bug--DonkeyMagic: Website design development http://www.donkeymagic.co.uk**The discussion list forhttp://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help**
Re: [WSG] addEventListener
Miika Mäkinen escribió: [...] Anyways, what I need to do is to add a onclick event handler to some links, and make this return false. My goal is that users with javascript turned on would go somewhere else than users without. Now, I need the click event to return false, so that javascript users will not folloq the link in the markup. But, for some reason a click event added with addEventListener doesn't seem to ever return false. Does anybody know how to do it? AttachEvent works fine (for IE). I can achieve this using element.onclick, I'd just like to use addEventListener for the sake of standards... [...] addEvent(a,click,function(){alert('not going there!');return false;}); [...] When clicked any of the links, the above example should in javascript-enabled clients display an alert not going there and NOT follow the link... but it doesn't work in either FireFox or Opera 8 (on windows XP). Work it the DOM way: event.preventDefault(); Unfortunately, IE doesn't support this method. So... function fixE(e) { if (!e) { e = window.event; }; if (!e.preventDefault) { e.preventDefault = function() { this.returnValue = false; }; }; if (!e.stopPropagation) { e.stopPropagation = function() { this.cancelBubble = true; }; }; return e; }; Use: addEvent(a, 'click', function(e) { e = fixE(e); alert('not going'); e.preventDefault(); }); HTH, Choan ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list getting help **