[thelist] Click once, Act Twice - SALIM

Christian Heilmann codepo8 at gmail.com
Mon Mar 20 04:50:26 CST 2006


You need to cancel the bubbling/default event and read the target:

	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e){
			e.stopPropagation();
			e.preventDefault();
		}
	},

	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		return target;
	},
So if your link would be <a id="foo" ...

you do a

var foo=document.getElementById('foo');
addEvent(foo,'click',fooListener,false);

and

fooListener:function(e){
  t=getTarget(e);  // get the link
  cancelClick(e);
}

Notice that Safari needs some special treatment as it returns the text
of the link and not the text as the target. Therefore you need to
check the nodeName:

	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}

		if (target.nodeName.toLowerCase() != 'a'){target = target.parentNode;}

		return target;
	},

Furthermore, Safari doesn't know cancelClick's stuff properly, which
is why you need to do a:

var foo=document.getElementById('foo');
addEvent(foo,'click',fooListener,false);
foo.onclick=function(){return false;}

HTH

Chris



More information about the thelist mailing list