SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2013
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can this be done? Anchors.

    If i have a menu with submenus like this:

    Code HTML4Strict:
    <ul>
    <li><a href="#">link</a>
      <ul>
         <a href="#anchor">anchorlink</a>
      </ul>
    </li>
    <li><a href="#">link</a>
      <ul>
         <a href="#anchor">anchorlink</a>
      </ul>
    </li>
    </ul>

    As it looks now, the anchors only work if you are on the correct page, but if I want it to work from other pages I would have to do something like for example "index.php#anchor". My question is if it is possible to add the page-name (like "index.php") to an anchor if you click an anchor from another page.

    like... if im on "about.php" and want to go to an anchor-link on "index.php".

    If I were to add "index.php" etc to each link manually, wouldnt that mean the page have to be reloaded every time i click an anchor, even if on the correct page?

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,198
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by ReGGaeBOSS View Post
    My question is if it is possible to add the page-name (like "index.php") to an anchor if you click an anchor from another page.
    Yes, certainly. That's what they are for. It means you can link to specific sections on other sites, too, if they have an id like that.

    If I were to add "index.php" etc to each link manually, wouldnt that mean the page have to be reloaded every time i click an anchor
    Yes.

    Be aware that your HTML is invalid. You can only have list items as direct children of a UL, so you need to change this

    Code:
    <ul>
         <a href="#anchor">anchorlink</a>
    </ul>
    to this

    Code:
    <ul>
         <li>
               <a href="#anchor">anchorlink</a>
         </li>
    </ul>

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,802
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    but what are you trying to do?

    Remember an ANCHOR will lead to a specific part of an HTML document where as a LINK will lead to a document on the web ( or for argument's sake.. within a site)

    There in lies the difference between: #, #anchor, page.php, and page.php#anchor.

    the first is generally a placeholder... really lending nowhere,
    the second will take you to the document: page.php
    the third will go to the element with the ID of "anchor" in THAT SAME DOCUMENT.
    and the last will take you to the element with the ID of "anchor" in the DOCUMENT named page.php.

    if you are doing a site navigation going from page to page you really don't need the anchor. if you are navigating within the same page, you really don't need the document name/ path


    hope that helps

  4. #4
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    If you're on index.php and you follow a link to index.php#anchor, I would not expect that to reload the page, but to just treat it like #anchor - so you shouldn't need to worry about forcing a reload by including the filename as well - but I'm happy to be corrected if I'm wrong on that one!

  5. #5
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dresden_phoenix View Post
    if you are doing a site navigation going from page to page you really don't need the anchor. if you are navigating within the same page, you really don't need the document name/ path
    I guess the main use case there would be if you've got included fragments, whether it's the nav menu or any other bit of content that is repeated across the site - you will need to have filename.php#anchor in that case, so that it works regardless of which page it is called from.

  6. #6
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I'm confused, is this a typo?

    There in lies the difference between: #, #anchor, page.php, and page.php#anchor.

    the first... [ok]
    the second will take you to the document: page.php
    the third will go to the element with the ID of "anchor" in THAT SAME DOCUMENT.


Bookmarks

Posting Permissions

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