www.webdeveloper.com
Results 1 to 7 of 7

Thread: Change location.hash (1) without history entry, and (2) without scrolling the page

  1. #1
    Join Date
    Feb 2006
    Posts
    65

    Change location.hash (1) without history entry, and (2) without scrolling the page

    I'm attempting to implement some deep-linking in my simple AJAX application by setting location.hash, and I've run into two problems:

    1) Sometimes setting location.hash seems to send the window on both IE and FF to scroll to the top of the page. I do not want any scrolling to occur. The location.hash change happens in a function which is triggered in an onclick event.

    2) Changing location.hash creates a history entry on FF (not on IE)... that's pretty cool for FF, but in this case I do not want a history entry created. How can this be avoided?

    I found an example which seems to have avoided both these problems, but cannot decipher the pertinent code:

    http://books.google.com/books?hl=en&...e7_NU#PPA24,M1

    Any help would be great. Thanks.

  2. #2
    Join Date
    Feb 2006
    Posts
    65
    No one?

  3. #3
    Join Date
    Feb 2006
    Posts
    65
    I'm still struggling with this if anyone has any idea...

  4. #4
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Changing the location.hash to "somevalue" is the JavaScript equivalent of someone clicking on a <a href="#somevalue"> link. It is supposed to jump them to that id within the current page.
    Stephen

  5. #5
    Join Date
    Feb 2006
    Posts
    65
    I'm not sure what you are trying to say, sorry.

    I am most certainly using anchors to "jump" the user to a certain state of the application, which is the idea of deeplinking. The state does not represent a physical vertical/horizontal scroll location on the page, though, it represents an overlay content box with dynamic data.

    In fact, this works fine. I set location.hash when the user clicks an anchor and I bring up the overlay content via javascript -- the hash gets changed without scrolling the page. The problem arises when I try to have a close button (an anchor) that simply hides the overlay content and resets the hash to something else. For some reason setting the hash from that anchor causes the page to scroll to the top.

    In both cases the hash does not reflect a real id anywhere on the page, they are both triggered in the same way by anchors, I can't tell why one works and the other doesn't -- I was hoping someone might have the knowledge but I'll keep comparing the two, there must be a difference.

  6. #6
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    I am most certainly using anchors to "jump" the user to a certain state of the application, which is the idea of deeplinking. The state does not represent a physical vertical/horizontal scroll location on the page, though...
    In both cases the hash does not reflect a real id anywhere on the page
    That is a total contradiction, and it appears your use of location.hash may be inappropriate since, as you say, you are not actually trying to jump to a different point on the page represented by an id in the hash.
    As felgall states:
    It is supposed to jump them to that id within the current page.
    Any other use of location.hash is improper. It IS common to use a # sign in an 'a' tag's href when using an onclick attribute in the 'a' tag, and when doing so the onclick should be returned as false to prevent the href's default action of following the url in the href, and page from jumping to the top of the page (which is default action when just a # sign in href, or the hash in href doesn't correspond to an actual id'd element).
    To demonstrate what I mean by 'returned as false', usually you have something like this:
    Code:
    <a href="#" onclick="return overlayContentToggle('wordYouHadInHash');">Toggle overlay</a>
    Note the onclick returns the function. Then your function which shows or hides the overlay should return false when the function exits:
    Code:
    function overlayContentToggle(myArgument) {
     //do something with myArgument
     //other code
     return false;
    }

  7. #7
    Join Date
    Dec 2010
    Posts
    1
    Quote Originally Posted by astupidname View Post
    That is a total contradiction, and it appears your use of location.hash may be inappropriate since, as you say, you are not actually trying to jump to a different point on the page represented by an id in the hash.
    As felgall states:

    Any other use of location.hash is improper.
    I have to wonder--does this person realize by now how ridiculous he was being, or is he still stomping around declaring every modern web application "improper" because webpages have advanced somewhat since the late 90s?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles