[thelist] JavaScript - adding onclick events to links that do not execute (advanced)

Tom Dell'Aringa pixelmech at yahoo.com
Fri Nov 25 10:46:39 CST 2005


Hi there,

I'm adding some onclick events to anchors/links using JavaScript during the onload. The onclick
should fire a function then return false so the href doesn't get followed. However, the return
false does not seem to fire/work in my anonymous function. Here is the code:

=======================================================
function addEventsToLinks()
{
	var filmstripItems = new Array();
	filmstripItems[0] = "colorado";
	filmstripItems[1] = "scene2";
	filmstripItems[2] = "scene3";

	var filmstrip = document.getElementById('filmstrip');
	var anchors = filmstrip.getElementsByTagName('a');
	
	for(var y=0; y<anchors.length; y++)
	{
		anchors[y].onclick = function() {
			ajaxController("load_full", ""+ filmstripItems[y] +"");
			return false;
		}
		anchors[y].href = "http://www.google.com/";
	}
}
window.onload = addEventsToLinks;


<div id="filmstrip">
<a href=""><img src="colorado_thumb.jpg" width="100" height="75" alt="" border="0" /></a>
<a href=""><img src="redrock1_thumb.jpg" width="100" height="75" alt="" border="0" /></a> 
<a href=""><img src="redrock2_thumb.jpg" width="100" height="75" alt="" border="0" /></a> 
</div>
=======================================================

Again the return false there doesn't seem to prevent the link from firing. Is there a way to
prevent the link from executing using this method?

TIA

Tom


http://www.pixelmech.com/
"I was in a convenience store reading a magazine and the clerk came up to me and said "This is not a library". so I said, "All right, I will talk louder then!"



More information about the thelist mailing list