Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop javascript onclick 'page jumping'

Can't seem to prevent the jump to top of page behavior.

I've tried everything suggested here preventing onclick page jumps

What else could be done to stop onclick page jumps?

Thanks.

Here's a snippet of the HTML and JS

HTML:

<a href="javascript: return null;" onclick='javascript:getPosts("page=1&display=all"); return false;'>Everyone</a>


JS:

function getPosts(qry) {
    var thePage = 'posts.php?' + qry;
    myReq.open("GET", thePage, true);
    myReq.onreadystatechange = theHTTPResponse;
    myReq.send(null);
}

function theHTTPResponse() {
    if (myReq.readyState == 4) {
        if(myReq.status == 200){
            var response = myReq.responseText;
            document.getElementById('posts').innerHTML = response;
        }
    } else {
        document.getElementById('posts').innerHTML = '<img src="images/ajax-loader.gif" />';
    }
}
like image 484
Bailey Avatar asked Apr 23 '26 14:04

Bailey


2 Answers

Returning false in the onclick is really the way to go. If that's not working for you, we need to see code.

like image 87
Aaron Avatar answered Apr 25 '26 03:04

Aaron


As far as I know, the events are already JS so, you dont need to put onclick="javascript:...". Try this:

<a href="javascript: return null;" onclick='getPosts("page=1&display=all"); return false;'>Everyone</a>
like image 28
Juan Avatar answered Apr 25 '26 03:04

Juan