views:

84

answers:

6

I have a link

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

when i click this link my page alway scroll up to the top. How do i manage it that when i clik this link my page not scroll up to the top.

Javascript? or something

thank you

+1  A: 

You might want to post an example of a link that does this. My guess is that it's because you don't have an href set for the link or you ended the link href with a "#someId"

It's not that it's scrolling to the top of the page, it's refreshing the page.

An example of a top link:

<a href="#header">Some Link</a>

<a href="#">Somewhere</a> <!-- will refresh and you end up at the top -->

EDIT Ah... Now that you've provided the link... it's the Hash # that's the problem.

To avoid that from happening ( I'm guessing you want to do some Javascript on the link and you're trying to get it to do something.. ) then you need return false; in your javascript. This will return false from the link and won't follow it.

Atømix
That is not 100% correct, the page gets not refreshed.
Felix Kling
True enough. However, it's more or less reloading the page. Or perhaps... restarting the page... by going to the top. To most users, it's the same thing.
Atømix
Seriously, there is no *more or less reloading* . Either a request is transmitted to the server or not. It is not. And we are not users here, we are developers and we have to stay technically correct ;)
Felix Kling
A: 
<body>
    <h1 id="top">First Headline</h1>

    <!-- your document here-->
    <a href="#top">go to Top</a>

</body>

With Javascript you could add some smoothness like slowly scroll up. HTML Links

gearsdigital
i don't go to top sir.
Giffary
Sorry for that... it seems body does not work. Try <h1 id="top"></h1> or something similar. This should work...
gearsdigital
See update above.
gearsdigital
A: 

It is because you have only the hash # as "URL". It makes the browser jump to the top of the page (normally it would jump to the element with the corresponding ID if you specify any).

But what is the purpose of such a link if you don't use it?

Felix Kling
A: 

The [relative] URL # is treated by browsers as the top of the page. Either change the link's href attribute to refer to another resource, or add a click event handler that prevents the default action. Better yet, if you intend it to be a button that triggers a click event, replace the <a> tag with a <button> which is more semantically correct anyway.

eyelidlessness
+3  A: 

you can add some javascript to deny the default behavior.

function myClickHandler(e) {

    // your code here
    // ...

    // new code
    if(e.preventDefault){ //firefox,chrome
        e.preventDefault();
    }
    else { // ie
        return false;
    }
}

if you provide some more detail/example code, we can give you a more specific answer.

lincolnk
+3  A: 

Not sure what you are trying to do, but maybe you are thinking of:

<a href="JavaScript:void(0);" >Text</a>

that'll do nothing.

Adam