Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor Tag with onclick/JavaScript attribute. Stop refreshing page in I.E

I have a HTML/JavaScript project that holds a few dozen anchor tags; each of the anchor tags calls the same JavaScript function, but with a different parameter.

Everything looks good in Firefox and Chrome, but in Internet Explorer (IE) the page seems to reload (flicker) every time I click an anchor tag (like the one shown below). How can I make IE stop reloading/flickering? I would prefer not to rewrite the whole script. I have tried onclcick='javascript... and href='javascript...,but both have the same problem (although onclick seems a little better).

<a onclick='javascript:foo(22)'></a> 
like image 935
John R Avatar asked Dec 21 '22 13:12

John R


2 Answers

Try <a onclick='foo(22); return false;'></a>

Also, javascript: is pointless in event attributes as it just defines a label.

like image 67
Shurdoof Avatar answered Dec 24 '22 02:12

Shurdoof


Simpler to use jQuery:

<a href="#" class="action" rel="22"></a>
<script>
    $('.action').click(function(){
        yourfunction($(this).attr('rel');
        return false;
    });
</script>
like image 28
Claudiu Avatar answered Dec 24 '22 01:12

Claudiu