Javascript Javascript使用哈希值重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10612438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript reload the page with hash value
提问by Jonas T
I am trying to refresh the page with this statement in external javascript file after some processes.
我正在尝试在一些过程后使用外部 javascript 文件中的此语句刷新页面。
window.location.href = window.location.href
It perfectly reload the page but I want to scroll to the specific location after reload. So, I put this on the page.
它完美地重新加载页面,但我想在重新加载后滚动到特定位置。所以,我把这个放在页面上。
<a name="uploadImgSection"></a>
And change the javascript to
并将javascript更改为
window.location.href = window.location.href + "#mypara";
This code doesn't reload/refresh the page either
此代码也不会重新加载/刷新页面
window.location.hash = "#uploadImgSection";
window.location.href = window.location.href;
When I do that, it doesn't reload the page at all. Is there any way I can reload the page with scroll bar position?
当我这样做时,它根本不会重新加载页面。有什么办法可以用滚动条位置重新加载页面?
回答by u283863
window.location.href += "#mypara";
location.reload();
First add the hash, then reload the page. The hash will remain there, and the page gets reloaded.
首先添加哈希,然后重新加载页面。散列将保留在那里,页面会重新加载。
Tested, works.
经测试,有效。
ps:If a hash already exist in the URL, you may want to directly change it via location.hash
instead of .href
.
ps:如果 URL 中已经存在哈希值,您可能希望直接通过location.hash
而不是来更改它.href
。
回答by ug_
I wanted to update this question because I found that using window.location.href += "#mypara"
will keep appending the parameter to the url even if it exists. I have found the better way is to use
我想更新这个问题,因为我发现 usingwindow.location.href += "#mypara"
会继续将参数附加到 url 中,即使它存在。我发现更好的方法是使用
window.location.hash = "#mypara"
location.reload();
回答by JayB
This is really an old post, I would still try and answer with right combination of answers.
这真的是一个旧帖子,我仍然会尝试用正确的答案组合来回答。
location.reload()
andlocation.reload(true)
works like F5 on browser. This will post all the form data back to the server if previously load had done it.location.href
will not refresh the page until there is a URL change recognized by the browser. change in hash (#paramname) doesn't qualify for URL change and hence just doinglocation.href+="#paramname"
will not work.
location.reload()
并location.reload(true)
在浏览器上像 F5 一样工作。如果之前加载完成,这会将所有表单数据发送回服务器。location.href
在浏览器识别出 URL 更改之前,不会刷新页面。散列中的更改 (#paramname) 不符合 URL 更改的条件,因此这样做是location.href+="#paramname"
行不通的。
So, location.href+="?anything#paramname"
should reload the page as a new request as ?anything
is change in the URL.
因此,location.href+="?anything#paramname"
应该像?anything
URL 中的更改一样将页面重新加载为新请求。
回答by Pandimanikandan A
var loc = location.hash;
location.hash = " ";
location.hash = loc;
回答by Kailash Chandra Panigrahi
This worked for me
这对我有用
$('body').on('click', '.className', function () {
var data = $(this).attr('href');
alert(data);
window.location.reload();
});
回答by MK Shah
This work for me
这对我有用
window.location.href = baseUrl + "#/m/team";
location.reload();
回答by Amit
This worked for me:
这对我有用:
var tabValue = document.URL;
window.location = tabValue.substring(0, tabValue.lastIndexOf("#"));
window.location.hash = "#tabs-3"; //Whatever is your hash value
location.reload();
回答by Hearaman
Try this
尝试这个
var urlString=window.location.href;
var url=urlString.split("#")[0];
window.location.href = url + "#mypara";