Stop javascript onclick 'page jumping'

It does not seem to stop you from skipping to the top of the page.

I tried everything offered here preventing page transitions

What else could be done to stop the transitions to the onclick page?

Thanks.

Here's a snippet of 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" />'; } } 
+2
javascript ajax onclick
source share
9 answers

Returning false in onclick is really the way to go. If this does not work for you, we need to see the code.

+4
source share

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

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

href="javascript:void(0);"

+2
source share
 <a href="javascript:;" onclick="...">Everyone</a> 

Pay attention to the semicolon.

+1
source share

this is what i use and it works

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

When it comes to ajax requests, using the function attached to the onClick event, since href always solves the problem of page forwarding for me.

+1
source share

I have always used:

 <a href=""></a> 

Mostly because I hate using

 <a href="#"></a> 

appends # to the end of the URL.

Edit:

Are you sure this is not a problem or browser preference?

0
source share

all you need is

 <a href="javascript:">My Link!</a> 

and add all the other attributes you need for the tag.

0
source share

Strangely, the problem was fixed after I deleted the contents of my php script, saved, and then re-entered the same content. Not sure what the effect is, but it seems to fix the jumps.

I think the problem never returned to onclick.

Thanks for every time!

0
source share

href = "javascript: undefined" should do the trick

0
source share

All Articles