Skip to main content

Modifying document.location.hash without page scrolling



We've got a few pages using ajax to load in content and there's a few occasions where we need to deep link into a page. Instead of having a link to "Users" and telling people to click "settings" it's helpful to be able to link people to user.aspx#settings





To allow people to provide us with correct links to sections (for tech support, etc.) I've got it set up to automatically modify the hash in the URL whenever a button is clicked. The only issue of course is that when this happens, it also scrolls the page to this element.





Is there a way to disable this? Below is how I'm doing this so far.







$(function(){

//This emulates a click on the correct button on page load

if(document.location.hash){

$("#buttons li a").removeClass('selected');

s=$(document.location.hash).addClass('selected').attr("href").replace("javascript:","");

eval(s);

}



//Click a button to change the hash

$("#buttons li a").click(function(){

$("#buttons li a").removeClass('selected');

$(this).addClass('selected');

document.location.hash=$(this).attr("id")

//return false;

});

});







I had hoped the return false; would stop the page from scrolling - but it just makes the link not work at all. So that's just commented out for now so I can navigate.





Any ideas?



Source: Tips4all

Comments

  1. Step 1: You need to defuse the node ID, until the hash has been set. This is done by removing the ID off the node while the hash is being set, and then adding it back on.

    hash = hash.replace( /^#/, '' );
    var node = $( '#' + hash );
    if ( node.length ) {
    node.attr( 'id', '' );
    }
    document.location.hash = hash;
    if ( node.length ) {
    node.attr( 'id', hash );
    }


    Step 2: Some browsers will trigger the scroll based on where the ID'd node was last seen so you need to help them a little. You need to add an extra div to the top of the viewport, set its ID to the hash, and then roll everything back:

    hash = hash.replace( /^#/, '' );
    var fx, node = $( '#' + hash );
    if ( node.length ) {
    fx = $( '<div></div>' )
    .css({
    position:'absolute',
    visibility:'hidden',
    top: $.scroll().top + 'px'
    })
    .attr( 'id', hash )
    .appendTo( document.body );
    node.attr( 'id', '' );
    }
    document.location.hash = hash;
    if ( node.length ) {
    fx.remove();
    node.attr( 'id', hash );
    }


    Step 3: Wrap it in a plugin and use that instead of writing to location.hash...

    ReplyDelete
  2. I think I may have found a fairly simple solution. The problem is that the hash in the URL is also an element on the page that you get scrolled to. if I just prepend some text to the hash, now it no longer references an existing element!

    $(function(){
    //This emulates a click on the correct button on page load
    if(document.location.hash){
    $("#buttons li a").removeClass('selected');
    s=$(document.location.hash.replace("btn_","")).addClass('selected').attr("href").replace("javascript:","");
    eval(s);
    }

    //Click a button to change the hash
    $("#buttons li a").click(function(){
    $("#buttons li a").removeClass('selected');
    $(this).addClass('selected');
    document.location.hash="btn_"+$(this).attr("id")
    //return false;
    });
    });


    Now the URL appears as page.aspx#btn_elementID which is not a real ID on the page. I just remove "btn_" and get the actual element ID

    ReplyDelete
  3. I don't think this is possible. As far as I know, the only time a browser doesn't scroll to a changed document.location.hash is if the hash doesn't exist within the page.

    This article isn't directly related to your question, but it discusses typical browser behavior of changing document.location.hash

    ReplyDelete
  4. lazyweb, lazy answer -
    why don't you just move the element you're linking to into view?
    have you tried using position:static?
    what happens when the specified id has display:none?

    ReplyDelete
  5. A snippet of your original code:

    $("#buttons li a").click(function(){
    $("#buttons li a").removeClass('selected');
    $(this).addClass('selected');
    document.location.hash=$(this).attr("id")
    });


    Change this to:

    $("#buttons li a").click(function(e){
    // need to pass in "e", which is the actual click event
    e.preventDefault();
    // the preventDefault() function ... prevents the default action.
    $("#buttons li a").removeClass('selected');
    $(this).addClass('selected');
    document.location.hash=$(this).attr("id")
    });

    ReplyDelete
  6. The other way to do this is to add a div that's hidden at the top of the viewport. This div is then assigned the id of the hash before the hash is added to the url....so then you don't get a scroll.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex