Prevent Default

I am loading a link with ajax. When the link pops on the screen and I click it, I get redirected to my 404 page and my lightbox doesn’t load.

If the link pops in and I refresh my browser, then I click the link my lightbox will show up.

How can I do a prevent default on the <a href> in pure JS? No frameworks please.

stopDef = function(e) {
 if (e && e.preventDefault)
   e.preventDefault();
 else if (window.event && window.event.returnValue)
   window.eventReturnValue = false;
 };

then just call stopDef() from within the JavaScript that is attached to the <a href>

That doesn’t seem to be working for me. What am I doing wrong?


if (info[x].reminder == 'assoc_requests')
{
	var a = document.createElement('a');
	var a2 = document.createElement('a');
	
	a.href = '/accountSettings.php?tab=6';
	a.title = 'Invite friends!';
	a.id = 'associateRequests';
	a.href = 'void(0)';
	
	a2.href = '/accountSettings.php?tab=6';
	a2.title = 'Invite friends!';
	a2.className = 'f_right';
	a2.id = 'associateRequests2';
	a2.href = 'void(0)';
	
	appendText(row, 'You have ');
	appendText(a, info[x].details);
	row.appendChild(a);
	
	if (info[x].details == 1)
	{
		appendText(row, ' Associate Request');
	}
	else
	{
		appendText(row, ' Associate Requests');
	}
	
	appendText(a2, 'View Associate Requests');
	row.appendChild(a2);
	
	list.appendChild(row);
	
	stopDef();
	
	stopDef = function(e) {
	 if (e && e.preventDefault)
	   e.preventDefault();
	 else if (window.event && window.event.returnValue)
	   window.eventReturnValue = false;
	 };
}

You need to define the stopDef function before you call it when you define it that way.

Even after I move the function call I still get redirected. Happen to have any other ideas?