JQuery.address how to remove # from url (javascript window.location remove #)

davidino picture davidino · Nov 25, 2010 · Viewed 12.4k times · Source

I need to remove the # from url when my event.value is == to /. I've got a lighbox with jquery.address that stores references to open images, when i close it i need to remove the # mark becouse this cause window scroll top.

I succed remove the # mark with this: window.location.href.slice(0, -1); but as you can see in the code above this cause the url rewrite when page is loaded and not only after my event.

How can i chain this javascript only when my complete happens, in the way this function is called olny when i close the lightbox.

I attach here my code with comments. thank you all

$.address.change(function(event) {
    curLink = event.value;
    if(curLink != '/') {

    // here my stuff, jquery.address generates url with reference 

    // ex: mysite.com/cat/subcat/page.html#one

    } else {  

        $('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {

        // here I need to remove the hash only after the complete 

        // ex: mysite.com/cat/subcat/page.html#  >  mysite.com/cat/subcat/page.html

        window.location.href.slice(0, -1);           
        $(this).hide(); 
    }});   
  }   
});

Answer

Phil.Wheeler picture Phil.Wheeler · Nov 25, 2010

Using window.location.hash = '' should remove it outright, otherwise there are some good plugins that open up a lot of functions for working with URLs (e.g. http://ajaxcssblog.com/jquery/url-read-request-variables).