Remove hash from url
Asked Answered
G

5

63

I am ajax-ifying the pagination in one of me projects and since I want the users to be able to bookmarks the current page, I am appending the page number via hash, say:

onclick="callPage(2); window.location.hash='p=2'; return false;"

and thats on the hyperlink it works fine and everything, except, when the page number is 1, i dont want to URL to be /products#p=1, I just want it to be /products

I tried these variations:

  1. window.location.hash='' works but the url is now like /products# and I dont quite the hash there.
  2. not using window.location.hash at all, but when the user comes back to page 1 from, say page 3, he is in page one, but url is still /products#p=3 since I am not messing with the hash.
  3. Google search on this led me to several minutes (about 15) of silly forums where the question was asked right, but answers were suggesting that the page jumps because the thread creator had a hash in href like <a href="#"> and he should use javascript:void(0) instead. (had they never heard of Ajax?)

So finally, I decided to make this thread, I found several similar threads here, but all the answers ls very similar to my second point.

so my big question still remains a question: How to kick the hash out of the URL and possibly out of the universe? (only for the first page!)

Generalist answered 22/12, 2010 at 11:13 Comment(7)
I see backticks are not valid in ordered lists.Generalist
@iamserious: They are, if you use Markdown and not HTML to create those lists ;)Cookie
@Kling, aha! this is neat, thanks!Generalist
what exactly is the problem with /products# ? Looks ok to meSnowdrift
@maksymko, I guess its just personal preference, but I would like a clean url, I mean, if there isn't anything after hash, why bother adding it in the first place? also, I was wondering if search bots look at /products and /products# as two different pages?Generalist
@Generalist I really doubt that search engines look at them differently. Since hash is never passed to the server it is immediately obvious, that content is the same. Empty hash indicates that the target position is the same. Our company's search even tries to determine if the links with different parameters are the same, and it is old as time, so I think modern engines can cope with that without any problems. I'd say this empty hash is more of a browser issue.Snowdrift
Possible duplicate of How to remove the hash from window.location (URL) with JavaScript without page refresh?Macready
R
68

Updated Answer:

Best way to achieve this is to follow Homero Barbosa's answer below:

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

... or, if you want to maintain the search parameters:

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

Original Answer, do not use this, badwrongfun:

var loc = window.location.href,
    index = loc.indexOf('#');

if (index > 0) {
  window.location = loc.substring(0, index);
}

... but that refreshes the page for you which seems a trifle rude after just arriving there. Grin and bear it seems to be the best option.

Royston answered 22/12, 2010 at 11:35 Comment(5)
You can also compromise and settle for something like /products#First which is bit more elegant than /products#p=1 :)Funches
this worked for me, you can also save the string again instead of changing the page: var hashIndex = userUrl.indexOf('#'); if (hashIndex > 0) { userUrl = userUrl.substring(0, hashIndex); }Chaussure
Below is a better answer!!Profiterole
history.pushState("", document.title, window.location.pathname); will also remove php query strings i.e. "?key=hello". How can we remove just the hash without removing the query string?Battista
I'm getting undefined name "history"Baseline
A
95
history.pushState("", document.title, window.location.pathname);
Addend answered 10/3, 2012 at 2:10 Comment(5)
just gave it a quick try, it's removing the query parameters too... not desirable!Generalist
thanks, just what I needed (although granted, I do not mind removing the query parameters (which can, however, be maintained with no problem by simply adding the query string, e. g. history.pushState('', document.title, window.location.pathname+window.location.search);))Diarthrosis
Good for new browsers, but IE9 and less doesn't support it. Just like to point that out.Ewers
This also creates a entry in the history (obviously). So when the user clicks the back button she will be on the same page.Enlistment
history.replaceState( ... ) would probably be more appropriate if you're worried about @Enlistment problemLogroll
R
68

Updated Answer:

Best way to achieve this is to follow Homero Barbosa's answer below:

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

... or, if you want to maintain the search parameters:

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

Original Answer, do not use this, badwrongfun:

var loc = window.location.href,
    index = loc.indexOf('#');

if (index > 0) {
  window.location = loc.substring(0, index);
}

... but that refreshes the page for you which seems a trifle rude after just arriving there. Grin and bear it seems to be the best option.

Royston answered 22/12, 2010 at 11:35 Comment(5)
You can also compromise and settle for something like /products#First which is bit more elegant than /products#p=1 :)Funches
this worked for me, you can also save the string again instead of changing the page: var hashIndex = userUrl.indexOf('#'); if (hashIndex > 0) { userUrl = userUrl.substring(0, hashIndex); }Chaussure
Below is a better answer!!Profiterole
history.pushState("", document.title, window.location.pathname); will also remove php query strings i.e. "?key=hello". How can we remove just the hash without removing the query string?Battista
I'm getting undefined name "history"Baseline
T
7

Worked For me Perfectly

$(window).on('hashchange', function(e){
  window.history.pushState("", document.title, window.location.pathname);  
 // do something...
});
Tenant answered 16/6, 2016 at 8:5 Comment(1)
Best and simplest answer, by far.Minimal
P
5
var urlWithoutHash = document.location.href.replace(location.hash , "" );
Pickering answered 27/9, 2013 at 6:0 Comment(1)
This has issues, like if the hash is a short word.Tingey
D
4
function removeHash () { 
    var scrollV, scrollH, loc = window.location;
    if ("pushState" in history)
        history.pushState("", document.title, loc.pathname + loc.search);
    else {
        // Prevent scrolling by storing the page's current scroll offset
        scrollV = document.body.scrollTop;
        scrollH = document.body.scrollLeft;

        loc.hash = "";

        // Restore the scroll offset, should be flicker free
        document.body.scrollTop = scrollV;
        document.body.scrollLeft = scrollH;
    }
}
Deafen answered 28/10, 2015 at 5:59 Comment(2)
Please, add explanation to your answer.Finger
This has been shamelessly copied from this answer.Ajani

© 2022 - 2024 — McMap. All rights reserved.