• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

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

0
vreten
Asked:
vreten
  • 11
  • 10
1 Solution
 
Michel PlungjanIT ExpertCommented:
Perhaps you tell us what you want to achieve. It could be that you do not need to do all this
0
 
vretenAuthor Commented:
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).
0
 
Michel PlungjanIT ExpertCommented:
Normally I would just do this:

<a href="#" onClick="this.href=whatever; return false">
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
vretenAuthor Commented:
Not able to alter the html just the JS (<script src="link_to_my_js" />)
0
 
Michel PlungjanIT ExpertCommented:
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 }
    }
  }
}
0
 
vretenAuthor Commented:
ok interesting but does links[i].onclick= ... work after event.preventDefault()? AND is it multi browser i.e. does it work in IE?
0
 
Michel PlungjanIT ExpertCommented:
My code is INSTEAD of preventdefault and works in all browsers
0
 
vretenAuthor Commented:
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.
0
 
Michel PlungjanIT ExpertCommented:
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>
0
 
vretenAuthor Commented:
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).
0
 
Michel PlungjanIT ExpertCommented:
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>
0
 
vretenAuthor Commented:
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"
0
 
Michel PlungjanIT ExpertCommented:
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;
     }
   }
  }
}
0
 
vretenAuthor Commented:
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.
0
 
Michel PlungjanIT ExpertCommented:
Not possible.
0
 
vretenAuthor Commented:
Is it possible to create your own new click event ("copy" of the original) and trigger a click?
0
 
Michel PlungjanIT ExpertCommented:
The code I have posted overwrites the click event. We can add to it if you need, but the technique is the same
0
 
vretenAuthor Commented:
please close...no sollution available
0
 
Michel PlungjanIT ExpertCommented:
Erm. Not possible is a valid answer. And worth more than a "C" or so

0
 
vretenAuthor Commented:
no sollution rather observation that its not possible
0
 
Michel PlungjanIT ExpertCommented:
Sure - so the solution is change the situation so it is possible or live with it.
Thanks

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now