Javascript redirect to hash from base url
The following is not working (I just get a reload):
location.host += "/#settings";
location.reload();
How do I tell JS to navigate to a URL based on the existing url and totally reload that page (not just append the hash)?
This also does not work:
window.location.href = "/#settings";
My base url is "http://localhost:sss/pricing"
I want to redirect to "http://localhost:sss/#settings"
with a reload.
I don't want to type localhost
anywhere.
Even this gives me settings#lol
:
var desiredBase = window.location.protocol + "//" + window.location.host + "/";
var path = '#lol';
window.location.href = desiredBase + path;
location.reload();
Yet another edit, stole base url code from here: How to get base url with jquery or javascript?
<html>
<body>
<h1>Hello Plunker!</h1>
<script>
(function(){
var getUrl = window.location;
var desiredBase = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
var path = `#lol`;
window.location.href = desiredBase + path;
location.reload();
})();
</script>
</body>
</html>
This is reloading the page locally after appending. Try it.
Edit: I see the issue with not getting the base url with href, working on it, hold on.
尝试这个
location.href += "/#settings";
location.reload();
You can try something like this :
location.hash = "settings";
This sets part after "#" in the URL
then do: location.reload();
and it should fix the issue
Let me know if it works
Thanks
链接地址: http://www.djcxy.com/p/42208.html