Solved

preventDefault and enableDefault

Posted on 2009-04-12
21
831 Views
Last Modified: 2012-06-27
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
Comment
Question by:vreten
  • 11
  • 10
21 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24128148
Perhaps you tell us what you want to achieve. It could be that you do not need to do all this
0
 

Author Comment

by:vreten
ID: 24145172
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24145293
Normally I would just do this:

<a href="#" onClick="this.href=whatever; return false">
0
 

Author Comment

by:vreten
ID: 24145643
Not able to alter the html just the JS (<script src="link_to_my_js" />)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24145923
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
 

Author Comment

by:vreten
ID: 24146009
ok interesting but does links[i].onclick= ... work after event.preventDefault()? AND is it multi browser i.e. does it work in IE?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24146084
My code is INSTEAD of preventdefault and works in all browsers
0
 

Author Comment

by:vreten
ID: 24146997
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24147016
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
 

Author Comment

by:vreten
ID: 24147116
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24147534
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
 

Author Comment

by:vreten
ID: 24147673
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24147797
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
 

Author Comment

by:vreten
ID: 24148236
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24148292
Not possible.
0
 

Author Comment

by:vreten
ID: 24156642
Is it possible to create your own new click event ("copy" of the original) and trigger a click?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24157177
The code I have posted overwrites the click event. We can add to it if you need, but the technique is the same
0
 

Author Comment

by:vreten
ID: 25578921
please close...no sollution available
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 25579367
Erm. Not possible is a valid answer. And worth more than a "C" or so

0
 

Author Closing Comment

by:vreten
ID: 31569333
no sollution rather observation that its not possible
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 25580717
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now