Link to home
Start Free TrialLog in
Avatar of vreten
vreten

asked on

preventDefault and enableDefault

I have a javascript that captures click events on "A" elements. If the click has occurred on a "A" element the event is stopped using element.preventDefault().  In my current sollution I use window.location.href just changing the target depending on case. The problem is that IE doesn't send referrer when using location.href (no problem with FF and others). Therefore I would prefer to "enable" the even after using preventDefault() instead of using location.href ... i.e. to let the event propagate as nothing has happend. Is that possible?
var l_el=(typeof event!='undefined')? event.srcElement : e.target;
var l_targetSite = "some_target_site";
 
if(l_el != null && l_el.tagName =="A"){
  if(e && e.preventDefault){
     e.preventDefault();
  } else {
    event.returnValue = false; // IE 6
  }
}
var l_origHref = l_el.href;
 
if(l_origHref == something){
  // do quite alot ... .. .
  window.location.href = l_target;
} else {
  window.location.href = l_origHref;
}

Open in new window

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Perhaps you tell us what you want to achieve. It could be that you do not need to do all this
Avatar of vreten
vreten

ASKER

I'm changing the DOM tree (adding elements) before passing on to a new target site. I have tried to just change l_el.href = l_target but that doesn't work if the user hits the back button (the anchor target will then be l_target instead of l_origHref).
Normally I would just do this:

<a href="#" onClick="this.href=whatever; return false">
Avatar of vreten

ASKER

Not able to alter the html just the JS (<script src="link_to_my_js" />)
window.onload=function() {
  var links = document.links; // or document.getElementsBytagName('a');
  for (var i=0, n=links.length; i<n;i++) {
    if (links[i].href.indexOf('some taget site') {
      links[i].href='somewhereelse'; // one method OR
//      links[i].onclick=function() { this.href='whatever'} // OR
//      links[i].onclick=function() { location='whatever'; return false }
    }
  }
}
Avatar of vreten

ASKER

ok interesting but does links[i].onclick= ... work after event.preventDefault()? AND is it multi browser i.e. does it work in IE?
My code is INSTEAD of preventdefault and works in all browsers
Avatar of vreten

ASKER

But I have noticed that in some browsers the click is forked in an parallel thread and thus happens before the JS have had time to execute. That's why I need preventDefault.
Huh?

Never heard of that.

Can you please tell me which browsers on what platform?

<a href="javascript:alert('Not executed')" onClick="alert('executed'); return false">Test</a>
Avatar of vreten

ASKER

Ok I would like to clarify that a bit :)

The thing is that when my if-statement is true I add an image to the DOM. The image sends a "web-bug" request that's registered at our server. If I don't stop the event from bubbling up (using preventDefault) the image request never "trigger". Probably because the click is followed through before the image has had time to execute. The only way I have found to prevent this is to "halt" the event.  I have a huge matrix of browser and don't remember exact model/version but i think it was IE 6 (usually is anyway).
Why add it to the DOM???


<a href="javascript:alert('Not executed')" onClick="var a = new Image(); a.src='somebug.asp?someparm='+escape(someparm)"; setTimeout('location=\'page2.html\'',100);return false">Test</a>
Avatar of vreten

ASKER

I have no control over the html src and can not change the anchor..just my included JS.
 I have tried both adding to the dom and creating a new element.
At the moment I have tried with
var l_image = new Image(\"0\",\"0\");
l_image.src = "new target";

also tried with different timeouts

but when i don't halt the event the problem remains the same...that is in some cases the image request is not tiggered before the "click"
window.onload=function() {
  var links = document.links; // or document.getElementsBytagName('a');
  for (var i=0, n=links.length; i<n;i++) {
    if (links[i].href.indexOf('some taget site') {
      links[i].onclick=function() {
      var a = new Image(); a.src='something';
      setTimeout('location="'+this.href+'"',100);
      return false;
     }
   }
  }
}
Avatar of vreten

ASKER

sorry but I don't think that will solve my problem. The original problem was that IE omits the referrer when using location.href. The image request problem is another thing. I have tested different approaches to that problem alot...the one you are suggesting is one of them.

But if i could get IE to send the referrer even if i use location.href we're home safe. Manually setting header referer parameter from JS or something like that.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vreten

ASKER

Is it possible to create your own new click event ("copy" of the original) and trigger a click?
The code I have posted overwrites the click event. We can add to it if you need, but the technique is the same
Avatar of vreten

ASKER

please close...no sollution available
Erm. Not possible is a valid answer. And worth more than a "C" or so

Avatar of vreten

ASKER

no sollution rather observation that its not possible
Sure - so the solution is change the situation so it is possible or live with it.
Thanks

https://www.experts-exchange.com/help.jsp#hs=29&hi=405