3

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

5 Answers 5

3

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

http://www.google.com/#top

to

http://www.google.com/
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

try this

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

console.log(newHash);

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('#', '');

console.log(newHash);

Comments

1

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';
};

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.