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

0 votes
asked Nov 25, 2010 by davidino

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);           

5 Answers

0 votes
answered Jan 25, 2010 by davidino

window.location.hash = '' don't work in my case.

window.location.href.slice(0, -1); works! the problem i can't solve is how I can chain this JavaScript instruction only after the complete jQuery event (by now it is fired on page load).

I think by now is stupid JavaScript call after jQuery

thank you

0 votes
answered Nov 25, 2010 by phil-wheeler

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).

0 votes
answered Jan 17, 2011 by eli

try this

newHash = window.location.hash.substring(1);


if you have an anchor then its usually appended at the beginning of the URL so but removing the first character then, you shouldn't have it as an issue anymore.

or if you can even do it this way which i think is what you might need

newHash = window.location.hash.replace('#', '');

0 votes
answered Jan 6, 2013 by browneyes

You could also use this (which totally removes the hash and cleans the URL right back up)

history.pushState("", document.title, window.location.pathname);

Which changes



0 votes
answered Jan 17, 2013 by bill-keller

I've found that changing the value after the hash worked best for a scenario similar to yours. It would need to be a hash that doesn't already exist on your page.

function removeDeepLink(event) {
    window.location.hash = 'foo';
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter