How to remove the hash from window.location (URL) with JavaScript without page refresh?
up vote
267
down vote
favorite
I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol # from the URL.
javascript window.location fragment-identifier
add a comment |
up vote
267
down vote
favorite
I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol # from the URL.
javascript window.location fragment-identifier
3
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
7
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
1
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53
add a comment |
up vote
267
down vote
favorite
up vote
267
down vote
favorite
I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol # from the URL.
javascript window.location fragment-identifier
I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol # from the URL.
javascript window.location fragment-identifier
javascript window.location fragment-identifier
edited Jul 10 '17 at 6:44
Rahul Gupta
5,64633249
5,64633249
asked Sep 9 '09 at 2:59
Tom Lehman
28.8k58178254
28.8k58178254
3
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
7
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
1
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53
add a comment |
3
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
7
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
1
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53
3
3
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
7
7
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
1
1
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53
add a comment |
13 Answers
13
active
oldest
votes
up vote
171
down vote
accepted
Initial question:
window.location.href.substr(0, window.location.href.indexOf('#'))
or
window.location.href.split('#')[0]
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...
MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
|
show 9 more comments
up vote
502
down vote
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
function removeHash () {
history.pushState("", document.title, window.location.pathname
+ window.location.search);
}
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
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;
}
}
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use replaceState() instead of pushState().
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
|
show 7 more comments
up vote
35
down vote
Simple and elegant:
history.replaceState({}, document.title, "."); // replace / with . to keep url
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
Thanks for this answer, I modified tohistory.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length));for my use case.
– Scott Jungwirth
Oct 16 '15 at 23:43
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
My use-case was also to replace instead of push, but this makes more sense to me:history.replaceState(null, document.title, location.pathname + location.search);
– MDMower
Oct 25 '17 at 6:57
|
show 3 more comments
up vote
22
down vote
(Too many answers are redundant and outdated.) The best solution now is this:
history.replaceState(null, null, ' ');
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
add a comment |
up vote
14
down vote
To remove the hash, you may try using this function
function remove_hash_from_url()
{
var uri = window.location.toString();
if (uri.indexOf("#") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("#"));
window.history.replaceState({}, document.title, clean_uri);
}
}
add a comment |
up vote
8
down vote
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
if(window.history.pushState) {
window.history.pushState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
add a comment |
up vote
4
down vote
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
add a comment |
up vote
4
down vote
const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
add a comment |
up vote
2
down vote
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
put this code on head section
add a comment |
up vote
2
down vote
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
add a comment |
up vote
1
down vote
Try the following:
window.history.back(1);
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
add a comment |
up vote
0
down vote
You can replace hash with null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
add a comment |
up vote
0
down vote
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
const hash = window.location.hash;
history.scrollRestoration = 'manual';
window.location.href = hash;
history.pushState('', document.title, window.location.pathname);
NOTE: The proposed API is now part of WhatWG HTML Living Standard
add a comment |
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
171
down vote
accepted
Initial question:
window.location.href.substr(0, window.location.href.indexOf('#'))
or
window.location.href.split('#')[0]
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...
MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
|
show 9 more comments
up vote
171
down vote
accepted
Initial question:
window.location.href.substr(0, window.location.href.indexOf('#'))
or
window.location.href.split('#')[0]
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...
MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
|
show 9 more comments
up vote
171
down vote
accepted
up vote
171
down vote
accepted
Initial question:
window.location.href.substr(0, window.location.href.indexOf('#'))
or
window.location.href.split('#')[0]
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...
MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
Initial question:
window.location.href.substr(0, window.location.href.indexOf('#'))
or
window.location.href.split('#')[0]
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...
MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
edited May 23 '17 at 12:34
Community♦
11
11
answered Sep 9 '09 at 3:06
Gabriel Hurley
32.3k105082
32.3k105082
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
|
show 9 more comments
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
18
18
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh.
– Evgeny
Oct 31 '10 at 20:21
57
57
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a refresh. "You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page)".
– Gabriel Hurley
Oct 31 '10 at 22:29
3
3
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
No page reload - that is in the question. This is not the answer as it requires/forces a page reload!
– Ed_
May 28 '12 at 16:56
8
8
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
also think it should not be the ✓answer
– abernier
Jun 19 '12 at 13:38
4
4
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
This is not an answer to the OP question.
– Bryce
Nov 14 '13 at 23:42
|
show 9 more comments
up vote
502
down vote
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
function removeHash () {
history.pushState("", document.title, window.location.pathname
+ window.location.search);
}
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
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;
}
}
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use replaceState() instead of pushState().
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
|
show 7 more comments
up vote
502
down vote
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
function removeHash () {
history.pushState("", document.title, window.location.pathname
+ window.location.search);
}
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
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;
}
}
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use replaceState() instead of pushState().
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
|
show 7 more comments
up vote
502
down vote
up vote
502
down vote
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
function removeHash () {
history.pushState("", document.title, window.location.pathname
+ window.location.search);
}
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
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;
}
}
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use replaceState() instead of pushState().
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
function removeHash () {
history.pushState("", document.title, window.location.pathname
+ window.location.search);
}
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
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;
}
}
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use replaceState() instead of pushState().
edited Jul 9 '14 at 9:32
answered Mar 14 '11 at 12:38
Andy E
259k65416417
259k65416417
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
|
show 7 more comments
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
23
23
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
this should be the best answer
– Mauro Zadunaisky
Mar 9 '12 at 21:38
8
8
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search);
– Phil Kulak
Mar 13 '12 at 15:56
6
6
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs.
– Andy E
Mar 13 '12 at 17:32
1
1
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048
– jasonmcclurg
Aug 14 '13 at 23:09
20
20
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history.
– Nico
Oct 6 '13 at 13:12
|
show 7 more comments
up vote
35
down vote
Simple and elegant:
history.replaceState({}, document.title, "."); // replace / with . to keep url
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
Thanks for this answer, I modified tohistory.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length));for my use case.
– Scott Jungwirth
Oct 16 '15 at 23:43
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
My use-case was also to replace instead of push, but this makes more sense to me:history.replaceState(null, document.title, location.pathname + location.search);
– MDMower
Oct 25 '17 at 6:57
|
show 3 more comments
up vote
35
down vote
Simple and elegant:
history.replaceState({}, document.title, "."); // replace / with . to keep url
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
Thanks for this answer, I modified tohistory.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length));for my use case.
– Scott Jungwirth
Oct 16 '15 at 23:43
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
My use-case was also to replace instead of push, but this makes more sense to me:history.replaceState(null, document.title, location.pathname + location.search);
– MDMower
Oct 25 '17 at 6:57
|
show 3 more comments
up vote
35
down vote
up vote
35
down vote
Simple and elegant:
history.replaceState({}, document.title, "."); // replace / with . to keep url
Simple and elegant:
history.replaceState({}, document.title, "."); // replace / with . to keep url
edited Jan 22 '17 at 15:23
Community♦
11
11
answered Jan 26 '15 at 18:02
cprcrack
9,81046172
9,81046172
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
Thanks for this answer, I modified tohistory.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length));for my use case.
– Scott Jungwirth
Oct 16 '15 at 23:43
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
My use-case was also to replace instead of push, but this makes more sense to me:history.replaceState(null, document.title, location.pathname + location.search);
– MDMower
Oct 25 '17 at 6:57
|
show 3 more comments
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
Thanks for this answer, I modified tohistory.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length));for my use case.
– Scott Jungwirth
Oct 16 '15 at 23:43
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
My use-case was also to replace instead of push, but this makes more sense to me:history.replaceState(null, document.title, location.pathname + location.search);
– MDMower
Oct 25 '17 at 6:57
3
3
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
use a dot instead of a slash if you want to stay in the same directory
– vahanpwns
Jun 11 '15 at 14:00
5
5
Thanks for this answer, I modified to
history.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length)); for my use case.– Scott Jungwirth
Oct 16 '15 at 23:43
Thanks for this answer, I modified to
history.replaceState({}, document.title, location.href.substr(0, location.href.length-location.hash.length)); for my use case.– Scott Jungwirth
Oct 16 '15 at 23:43
2
2
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
How could this possibly get 6 upvotes?
– MrUpsidown
Jan 14 '16 at 20:02
5
5
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
This does not preserve url query.
– Vladislav Kostenko
Jun 26 '17 at 20:15
2
2
My use-case was also to replace instead of push, but this makes more sense to me:
history.replaceState(null, document.title, location.pathname + location.search);– MDMower
Oct 25 '17 at 6:57
My use-case was also to replace instead of push, but this makes more sense to me:
history.replaceState(null, document.title, location.pathname + location.search);– MDMower
Oct 25 '17 at 6:57
|
show 3 more comments
up vote
22
down vote
(Too many answers are redundant and outdated.) The best solution now is this:
history.replaceState(null, null, ' ');
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
add a comment |
up vote
22
down vote
(Too many answers are redundant and outdated.) The best solution now is this:
history.replaceState(null, null, ' ');
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
add a comment |
up vote
22
down vote
up vote
22
down vote
(Too many answers are redundant and outdated.) The best solution now is this:
history.replaceState(null, null, ' ');
(Too many answers are redundant and outdated.) The best solution now is this:
history.replaceState(null, null, ' ');
answered Mar 19 at 22:54
Pacerier
43.2k50206511
43.2k50206511
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
add a comment |
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
THE answer. Thanks a lot for the simplest and newest answer.
– Mayeenul Islam
Nov 7 at 16:54
add a comment |
up vote
14
down vote
To remove the hash, you may try using this function
function remove_hash_from_url()
{
var uri = window.location.toString();
if (uri.indexOf("#") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("#"));
window.history.replaceState({}, document.title, clean_uri);
}
}
add a comment |
up vote
14
down vote
To remove the hash, you may try using this function
function remove_hash_from_url()
{
var uri = window.location.toString();
if (uri.indexOf("#") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("#"));
window.history.replaceState({}, document.title, clean_uri);
}
}
add a comment |
up vote
14
down vote
up vote
14
down vote
To remove the hash, you may try using this function
function remove_hash_from_url()
{
var uri = window.location.toString();
if (uri.indexOf("#") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("#"));
window.history.replaceState({}, document.title, clean_uri);
}
}
To remove the hash, you may try using this function
function remove_hash_from_url()
{
var uri = window.location.toString();
if (uri.indexOf("#") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("#"));
window.history.replaceState({}, document.title, clean_uri);
}
}
edited Feb 7 '17 at 8:13
answered Feb 7 '17 at 8:05
Rahul Gupta
5,64633249
5,64633249
add a comment |
add a comment |
up vote
8
down vote
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
if(window.history.pushState) {
window.history.pushState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
add a comment |
up vote
8
down vote
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
if(window.history.pushState) {
window.history.pushState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
add a comment |
up vote
8
down vote
up vote
8
down vote
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
if(window.history.pushState) {
window.history.pushState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
if(window.history.pushState) {
window.history.pushState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
answered Aug 17 '16 at 18:05
Chris Gunawardena
3,3371627
3,3371627
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
add a comment |
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
This removes any query params present in the URL :(
– kevgathuku
Aug 2 at 11:53
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
@kevgathuku you can add them back with location.search, eg: ` window.history.pushState('', '/', window.location.pathname + window.location.search)`
– Chris Gunawardena
Aug 2 at 12:04
add a comment |
up vote
4
down vote
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
add a comment |
up vote
4
down vote
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
add a comment |
up vote
4
down vote
up vote
4
down vote
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
edited Feb 7 '15 at 18:03
answered Feb 7 '15 at 17:58
Nibras Reeza
7914
7914
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
add a comment |
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
6
6
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
Setting hash to whitespace still keeps the # at the end of the URL, I think the goal is to remove it altogether.
– mahemoff
Feb 4 '16 at 12:18
1
1
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
It trails a hash at the end of the URL. The question is how to remove that.
– Gurmeet Singh
Sep 11 '17 at 5:56
add a comment |
up vote
4
down vote
const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
add a comment |
up vote
4
down vote
const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
add a comment |
up vote
4
down vote
up vote
4
down vote
const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
answered Apr 2 at 6:50
Casey
1,10921433
1,10921433
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
add a comment |
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
2
2
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
– rollstuhlfahrer
Apr 2 at 7:07
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– SilverNak
Apr 2 at 7:09
add a comment |
up vote
2
down vote
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
put this code on head section
add a comment |
up vote
2
down vote
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
put this code on head section
add a comment |
up vote
2
down vote
up vote
2
down vote
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
put this code on head section
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
put this code on head section
answered Jan 7 '16 at 16:58
Manigandan Raamanathan
211
211
add a comment |
add a comment |
up vote
2
down vote
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
add a comment |
up vote
2
down vote
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
add a comment |
up vote
2
down vote
up vote
2
down vote
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
answered Sep 23 at 12:53
METALHEAD
304112
304112
add a comment |
add a comment |
up vote
1
down vote
Try the following:
window.history.back(1);
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
add a comment |
up vote
1
down vote
Try the following:
window.history.back(1);
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
add a comment |
up vote
1
down vote
up vote
1
down vote
Try the following:
window.history.back(1);
Try the following:
window.history.back(1);
edited Jan 15 '16 at 15:03
josliber♦
37k115996
37k115996
answered Jul 13 '13 at 10:10
Vishal Sharma
913
913
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
add a comment |
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
yeah its working thanx @Vishal Sharma
– Jimmy
Jul 27 '13 at 7:35
21
21
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This doesn't answer the question if the user has come directly to the URL including the hash.
– nickb
Jan 5 '14 at 2:57
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
This trick is very useful when you just want create a link to previous page, but the link is hidden in bunch of js files.
– ValidfroM
Apr 15 '14 at 16:55
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
Exactly what I was looking for. This works perfectly to remove a hashtag just created by my web app in order to consume a value returned by a javasript function.
– user278859
Jun 7 at 5:53
add a comment |
up vote
0
down vote
You can replace hash with null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
add a comment |
up vote
0
down vote
You can replace hash with null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
add a comment |
up vote
0
down vote
up vote
0
down vote
You can replace hash with null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
You can replace hash with null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
answered Sep 27 '13 at 6:04
Devang Bhagdev
3851513
3851513
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
add a comment |
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
1
1
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
The question asks "without causing the page to refresh", but this method does refresh the page. You should note this fact in your answer, so we don't all have to waste our time finding out firsthand that you're answering a different question than the one we're actually trying to get answered here.
– Vladimir Kornea
Jan 9 '15 at 0:41
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
It does not refresh the page.
– Ciprian
Jul 1 '16 at 10:08
add a comment |
up vote
0
down vote
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
const hash = window.location.hash;
history.scrollRestoration = 'manual';
window.location.href = hash;
history.pushState('', document.title, window.location.pathname);
NOTE: The proposed API is now part of WhatWG HTML Living Standard
add a comment |
up vote
0
down vote
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
const hash = window.location.hash;
history.scrollRestoration = 'manual';
window.location.href = hash;
history.pushState('', document.title, window.location.pathname);
NOTE: The proposed API is now part of WhatWG HTML Living Standard
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
const hash = window.location.hash;
history.scrollRestoration = 'manual';
window.location.href = hash;
history.pushState('', document.title, window.location.pathname);
NOTE: The proposed API is now part of WhatWG HTML Living Standard
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
const hash = window.location.hash;
history.scrollRestoration = 'manual';
window.location.href = hash;
history.pushState('', document.title, window.location.pathname);
NOTE: The proposed API is now part of WhatWG HTML Living Standard
answered Nov 29 '17 at 14:44
voscausa
7,91711945
7,91711945
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1397329%2fhow-to-remove-the-hash-from-window-location-url-with-javascript-without-page-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Do you really want to do this? It'll cause a page refresh.
– seth
Sep 9 '09 at 3:08
7
Is it possible to do without a page refresh?
– Tom Lehman
Sep 9 '09 at 3:11
1
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into.
– Gabriel Hurley
Sep 9 '09 at 3:14
Is there any conciderations with leaving a "#" behind other than a visual one?
– user29660
Aug 16 '16 at 10:55
Possible duplicate of Modify the URL without reloading the page
– PayteR
Sep 10 '17 at 12:53