Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid page refreshing on anchor (<a></a>) tag click?

I'm creating a dynamic website. My problem is when i click on the following tag:

<a class="s-inte" href="set_interesantes.php?n=Frank Melo&u=f6e79cfe9c0ecc4c08dac4c860c4802b&back=http://localhost:8085/Something/success/profile.php?search_user=f6e79cfe9c0ecc4c08dac4c860c4802b&p=12&sa=f6e79cfe9c0ecc4c08dac4c860c4802b&i=2345123&dl=&iv=1">Interesante</a>

The page gets refreshed, How do I avoid this page refresh?

like image 621
Pepe Perez Avatar asked Mar 21 '23 19:03

Pepe Perez


2 Answers

What you want to accomplish is to update some counter of interestings w/o refreshing the page? You should do it using AJAX techniques, this is what AJAX was invented for.

Consider the following code, it's top easy (jQuery library required):

<a href="#" class="counter">Interesante</a>

<script>
$(function(){
    $("a.counter").click(function()
    {
         $.get("set_interesantes.php?n=Frank Melo&u=f6e79cfe9c0ecc4c08dac4c860c4802b&back=http://localhost:8085/Something/success/profile.php?search_user=f6e79cfe9c0ecc4c08dac4c860c4802b&p=12&sa=f6e79cfe9c0ecc4c08dac4c860c4802b&i=2345123&dl=&iv=1" );
         .... // you can do some animation here, like a "Liked!" popup or something
         return false; // prevent default browser refresh on "#" link
    });
});
</script>
like image 139
Oleg Dubas Avatar answered Mar 23 '23 08:03

Oleg Dubas


you need to prevent default action on the click event.

You can do a simple inline handler which will return false

<a class="s-inte" onclick="return false" href="set_interesantes.php?n=Frank Melo&u=f6e79cfe9c0ecc4c08dac4c860c4802b&back=http://localhost:8085/Something/success/profile.php?search_user=f6e79cfe9c0ecc4c08dac4c860c4802b&p=12&sa=f6e79cfe9c0ecc4c08dac4c860c4802b&i=2345123&dl=&iv=1">Interesante</a>

or write a jQuery handler which will do the same

$('.s-inte').click(function(e){
    e.preventDefault()
})
like image 32
Arun P Johny Avatar answered Mar 23 '23 08:03

Arun P Johny