SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Positioning in Page Without # in URL

    I'm working on a PHP app, adding comments functionality. The goal is to refresh the page (i.e. reload it), and position it at the appropriate spot (below the fold), but without specifying a # in the URL. I want the user to be able to save the URL without being brought back to exactly the same spot.

    I epxect there is a way to do this; possibly with JavaScript. After all, when you hit a link which points within the same page, it doesn't reload the page; it just jumps to where it's supposed to go.

    Any help would be appreciated.
    - A. Alfred Ayache
    PHP/MySQL ZCE

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though I've never done this before, I'd assume you can do this by calculating the top offset of the element which you want to link to using a function such as gavin's findxy function, and then using the scrollTo method of the window object to get there.
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks, SoulScratch. After looking into it some, it turns out the intra-page link does add a #link to the URL after all... So I may as well just do it that way, and trust my users to know how to save a bookmark.
    - A. Alfred Ayache
    PHP/MySQL ZCE

  4. #4
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    OK, so I linked to a URL like http://www.example.com/mypage.html?a=1&b=2#name. It went to the page, got the parameters, but didn't jump to the named anchor.

    This was in FF 3. IE 7 worked fine. Chrome didn't go.

    And yet, phpBB goes to the bottom of the page when you go to the last post in a thread.

    Anyone have any idea why this would be?

    Thx.
    - A. Alfred Ayache
    PHP/MySQL ZCE

  5. #5
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I may have an idea why it isn't jumping there.

    The named anchor is actually hidden when the page is first loaded. One of the params indicates the tab should be displayed, but this only happens via JS after the page is loaded.

    Bummer.

    I may need to play around with this.

    Oh well, it's not a deal breaker.

    Thanks for listening.
    - A. Alfred Ayache
    PHP/MySQL ZCE

  6. #6
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that was it! The named anchor has to have a visible display property (i.e. not none).
    - A. Alfred Ayache
    PHP/MySQL ZCE

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by alfred3x View Post
    Yep, that was it! The named anchor has to have a visible display property (i.e. not none).
    By named anchor you mean the id of the element you want placed at the top of the page right?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    By named anchor you mean the id of the element you want placed at the top of the page right?
    Sorry, no: I meant like <a name="commentform"></a>. Then you can link to it with http://www.example.com/mypage.html?a=1&b=2#commentform

    Perhaps I used the wrong name for the type of anchor.

    OK, just checked out the w3.org page on Links in HTML. (http://www.w3.org/TR/REC-html40/stru....html#h-12.1.1) They're called destination anchors, and you can do it with either a name attribute in an anchor tag, or an id attribute in any tag. I didn't know about that second one.
    - A. Alfred Ayache
    PHP/MySQL ZCE

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    <a name=something"></a> was deprecated in favour of <tag id="something"> quite some time ago. The last browser in common use to require the <a name> instead of just an id was Netscape 4 so the need for the <a name> tag ended about 5 years ago. Even back then there were some browsers that didn't recognise that properly and so an id with the same value as the name was needed to ensure proper cross browser support.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •