Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop href link after onClick function is called

Tags:

html

jquery

ajax

I have a link:

<h2><a href=?p=article&aid='.$article->id.'" onclick="loadPage('article'); return false;">.$article->title.</a></h2>

and it calls this function:

function loadPage(page){
    $("#content").html("Loading...");

    $.post("page.php", {p: page}, function(data){
        $("#content").html(data);   
    });
}

But after my javascript has been ran the href is still active. I've used return false; on my onClick's before to prevent this but it's not working this time????

like image 353
Undefined Avatar asked May 29 '26 23:05

Undefined


1 Answers

It looks like your script has an error and is not reaching the return block. If you add the try catch, it will ignore the errors in the script.

function loadPage(page){
try{
    $("#content").html("Loading...");

    $.post("page.php", {p: page}, function(data){
        $("#content").html(data);   
    });
} catch(err){}
}
like image 122
Phil Avatar answered Jun 01 '26 12:06

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!