1

I have this type of hashing URLs,

http://mysite.com/projects/#/article-1/
http://mysite.com/projects/#/article-2/
http://mysite.com/projects/#/article-2/

And I want to remove the hash bit (the fragment) when I click on a button on the page without redirecting the page.

$('.back').click(function(){


    // remove the fragment.
    // don't know how to make it..
    return false;

});

So I can get this only,

http://mysite.com/projects/

How can I do that?

Thanks.

EDIT: How does this site make it? If you click close project on the ajax loaded page, eg -

http://www.pentagram.com/work/#/all/all/newest/2534/

you will get this back,

http://www.pentagram.com/work/#/all/all/newest/
4
  • Are you using anchors? What you need to do with the first substring? Commented Jun 27, 2011 at 16:41
  • no. I am loading an ajax page using location.hash and I want to remove it when I remove that ajax page. Commented Jun 27, 2011 at 16:44
  • Regarding your update: If you look closely you see that it does not remove the hash. It just removes part of the hash. Commented Jun 27, 2011 at 18:24
  • @Felix: thanks for pointing that out. I realised that after the edit! lol Commented Jun 27, 2011 at 21:06

3 Answers 3

2

The short answer is:

window.location.hash = '';

This will leave you with a trailing # though.

Sign up to request clarification or add additional context in comments.

3 Comments

@Flix Kling history.pushState is only supported in Firefox, Chrome, and Safari at the moment.
@timw4mail: I know. One would have to test whether it is supported of course.
@lauthiamkok: It does not redirect. Jumping is different than redirect. Redirection means that the browser issues an HTTP request and loads a new page.
1

If I understand you right, you can try to mimic what would normally happen, I mean:

  • on click prevent default action
  • analyze the link, get tha hash part
  • find tha anchor on the page (y coordinate)
  • scroll to the anchor

Something like this:

$("a").click(function() {
    var loc = $(this).attr("href").split("#"),
        hash = loc[1] ? loc[1] : null;
    if (hash && $("a[name="+hash+"]").size() > 0)
    {
        var pos = $("a[name="+hash+"]").position();
        $(window).scrollTop(pos.top);
        return false;
    }
});

This way tha url remains the same, but you have the functionality.

1 Comment

no, that is not what I meant actually sorry. have a look on my edit please. thanks.
0

I'm pretty sure this isn't possible. You can get rid of everything after the hash by setting the location hash like this, though:

location.hash = '';

1 Comment

In that case you'll need to remove your onhashchange event before you change the hash.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.