Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Possible to stop page jumping when # link is used?

    Hello,

    When I use an <a href> link internally with #, the page jumps to the top. Is there any way to stop this?

    Thanks

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    341
    Thanks
    3
    Thanked 36 Times in 36 Posts
    Yep, don't include the href part in the <a>. So your link will look like this, for the time being:

    Code:
    <a>Link text</a>
    Frank

    How to: Target IE in, Position in, Center in, Create a Fixed ('Sticky') Footer with, and Create a Drop-Down/Fly-Out Menu with CSS: Website Laten Maken Amsterdam.

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Location
    CA
    Posts
    105
    Thanks
    0
    Thanked 10 Times in 10 Posts
    What I did was give the <a href> an id. Then the id went into the #:

    <a href="#something" id="something">link is here</a>

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by davidwhite View Post
    Hello,

    When I use an <a href> link internally with #, the page jumps to the top. Is there any way to stop this?

    Thanks
    Better would be not to use a link, if it is not linking to anything..
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Where you see code that reads <a href="#">something</a> the # is supposed to be replaced with whatever page you intend to have the link take people to if it isn't overriden by JavaScript - you should never actually use it in a live web page. If it isn't intended to be a link to anywhere for those without JavaScript then don't code an <a> at all in the HTML.

    The one time when you might actually set the href to # is if the link is added from JavaScript and the script overrides the default action and you want the href there so that the browser adds it to the link collection and not the anchor collection. For example:

    Code:
    var a = document.createElement('a');
    a.href="#";
    a.onclick = function() {functiontorun(); return false;};
    a.innerHTML = 'click here to run the function';
    document.getElementById('addPoint').appendChild(a);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Nov 2012
    Location
    India
    Posts
    53
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Please remove that # sign.

  • #7
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    Code:
    <a href="javascript:void(0)" onclick="Index">

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by tempz View Post
    Code:
    <a href="javascript:void(0)" onclick="Index">
    This approach is discouraged, and "javascript:" is deprecated/not required.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •