views:

1051

answers:

9

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

I've tried everything suggested here http://stackoverflow.com/questions/1631924/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" />';
    }
}
+3  A: 

If returning false in the onclick attribute isn't working, there's something else going on. The solutions posted there do work in a normal situation, so we'll need to see your code.

ceejayoz
+2  A: 

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

Tegeril
A: 

I've always used:

<a href=""></a>

Mostly because I hate that using

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

appends a # to the end of the url.

Edit:

Are you sure it isn't a browser specific problem or preference?

Sneakyness
You do not get the # appended to the URL if you return false in the onclick.
Tegeril
In this case, it's better to not use anchor tags at all. If the element isn't *actually* a link, don't make it one. Use CSS to style a `<span>` (or whatever) to mimic a link's appearance if that's the goal.
Peter Bailey
@Tegeril, Thanks, I've tried those and I still get the same behaviour.@Peter Bailey, I've tried using span instead of anchors, ie. <span onClick('...')>blah</span>, still not stopping the page jump.Looks like I need ot look into this deeply, just not sure where to start. My php script returns normal html tags
Bailey
@Sneaky, definitely something going on in my code, I've tested all workarounds in IE and FF.
Bailey
Safari? Opera? I don't care about those two.
Sneakyness
A: 

all you need is

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

and add whatever other attributes you need to the tag.

Jason
Thanks Jason, tried that too, but the onclick is still making it "jump". :(
Bailey
have you tried using a jquery `$.post` instead of the way you're doing it?
Jason
...expanding, using jquery you don't have to use the `OnClick` attribute at all, so that will get rid of your page jump entirely...
Jason
Thanks, I'll look into it, I'm new to web dev in general, this was just a straight forward 'ajax' example i was going through, haven't gotten around to jquery.
Bailey
@bailey - !!! you need to get on that immediately!! it will make your life soooo much easier, and you will learn normal JS as a byproduct :) to make a standard ajax call in jquery: `$.post('example.com', data, function() { callback logic });` SOOOO easy
Jason
@jason: Although jquery makes things much easier, it is a good idea to know how things work before hand.
MitMaro
@mit - notice where i said you will learn normal JS as a byproduct... jquery was my way into javascript, and now i know 29038520359x more about JS than i did before i was introduced to jquery.
Jason
+1  A: 

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>
Juan
A: 
<a href="javascript:;" onclick="...">Everyone</a>

Note the semicolon.

i-g
A: 

Strangely, the problem was corrected after I deleted the contents of my php script, saved, then reinserted the same content. Not sure what effect this had but seems to fix the jumping.

I guess the issue was never with returning false in onclick.

Thanks for everyone's time!

Bailey
A: 

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 that is tied to the 'onClick' event as the href always solves the page jumping problem for me.

Q_the_dreadlocked_ninja
A: 

href="javascript:void(0);"

MRT