How to replace 开发者_JAVA技巧a url like this
http://test.com/#part1
to:
http://test.com/part1
I know location.hash but it will detect if there is a hash in url.
location.href = location.href.replace(location.hash,location.hash.substr(1))
You can use replace()
Here's a broken down version using windows.location
:
var new_url = window.location.protocol + '//'
+ window.location.hostname + '/'
+ window.location.pathname + '/'
+ window.location.hash.replace('#','','g') ;
Or remove all the hashes:
var new_url = (window.location + '').replace('#','','g');
var file = location.pathname.substring(location.pathname.lastIndexOf("/") + 2);
var location = window.origin + "file";
window.location = location;
in my opinion is using regex replace on string best and cleanest solution
.replace( /#.*/, "");
example
location.href = location.href.replace( /#.*/, "");
精彩评论