3

I can not seem to get IE8 to prevent the onclick function from executing. Tried numerous code variations. Any help would be appreciated. As it stands now when I click on the link I get the alert popup which should not be happening.

 <p><a href="#" onclick="openMoreProductVideos('Video title','videocode','false');return false;"><span class="txt">View all videos</span></a></p>

function openMoreProductVideos(title,productCode,fastprevoew) 
{  
  alert ("openMoreProductVideos - should not see this!");
}

$('a[onclick^=openMoreProductVideos]').click(function (event) {
   if (event.stopPropagation){
         event.stopPropagation();
   }
   else if(window.event){
      window.event.cancelBubble=true;       
   }       
    return false;
});
casperOne
  • 73,706
  • 19
  • 184
  • 253
septemberbrain
  • 998
  • 9
  • 25
  • what does `alert(event.stopPropagation);` show? And why do you need two event handlers in your code? – Mark Schultheiss Jun 13 '12 at 16:54
  • [This](http://stackoverflow.com/questions/1756425/prevent-onclick-action-with-jquery/) will help and [this](http://stackoverflow.com/questions/387736/how-to-stop-event-propagation-with-inline-onclick-attribute/) too :) – Jashwant Jun 13 '12 at 17:37

3 Answers3

1

just try this..removing the other conditions..

$('a[onclick^=openMoreProductVideos]').click(function (event) {

   event.preventDefault();      //will prevent the event from occuring


});

check the jsfiddle ..jsfiddle.net/kabichill/qu7kP

Kabilan S
  • 1,104
  • 4
  • 15
  • 31
0

As I understand it, stopPropagation() and cancelBubble are used to stop the event from going further up the chain of listeners, but will still execute the current listener. Have you tried event.preventDefault()?

chrisfrancis27
  • 4,516
  • 1
  • 24
  • 32
  • yes and that does not work either. I think what needs to happen is the eventhandler needs to be removed first but not sure how to get that working for IE8. for example, the following code works for ff and chrome but not for IE. $('a[onClick^=openMoreProductVideos]').each(function (e) { this.onclick = undefined; }); – septemberbrain Jun 13 '12 at 16:38
0

This works fine for me in IE8+ & FF

function stopEvent(e) {    
    if (!e) e = window.event;

    if (e.cancelBubble != null) e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation(); //e.stopPropagation works in Firefox.
    if (e.preventDefault) e.preventDefault();
    if (e.returnValue != null) e.returnValue = false; // http://blog.patricktresp.de/2012/02/
    return false;
}

IMP : FYI, I got stuck with if(e.cancelBubble) for IE, it has to be if(e.cancelbubble != null)

Hemant Tank
  • 1,724
  • 4
  • 28
  • 56