Html 重新加载当前页面的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8174282/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:33:23  来源:igfitidea点击:

Link to reload current page

htmlhyperlink

提问by Tyilo

Is it possible to have a normal link pointing to the current location?

是否有可能有一个指向当前位置的正常链接?

I have currently found 2 solutions, but one of them includes JavaScript and in the other you have to know the absolute path to the page:

我目前找到了 2 个解决方案,但其中一个包含 JavaScript,另一个您必须知道页面的绝对路径:

<a href="#" onclick="window.location.reload(true);">1</a>
<a href="/foobar/">2</a>
<a href="#">3 (of course not working)</a>

Is there any way of doing this, without using JavaScript or knowing the absolute path?

有没有办法做到这一点,而不使用 JavaScript 或不知道绝对路径?

回答by Markus Amalthea Magnuson

I have been using:

我一直在使用:

<a href=".">link</a>

Have yet to find a case and/or browser where it does not work as intended.

尚未找到无法按预期工作的案例和/或浏览器。

Period means the current path. You can also use ..to refer to the folder above the current path, for instance, if you have this file structure:

Period 表示当前路径。您还可以使用..引用当前路径上方的文件夹,例如,如果您有以下文件结构:

page1.html
folder1
    page2.html

You can then in page2.htmlwrite:

然后你可以page2.html写:

<a href="../page1.html">link to page 1</a>

EDIT:

编辑:

I'm not sure if the behaviour has changed or if it was always like this, but Chrome (and maybe others) will treat periods as described above as regarding directories, not files. This means that if you are at http://example.com/foo/bar.htmlyou are really in the directory /foo/and a hrefvalue of .in bar.htmlwill refer to /foo/rather than bar.html

我不确定行为是否已经改变,或者它是否总是这样,但 Chrome(可能还有其他人)会将上述句点视为关于目录,而不是文件。这意味着,如果你在http://example.com/foo/bar.html你真的在目录/foo/href.bar.html将参考/foo/而不是bar.html

Think of it as navigating the file system in a terminal; you can never cdinto a file :)

可以将其视为在终端中导航文件系统;你永远不能cd进入一个文件:)

EDIT 2:

编辑2:

It seems like the behaviour of using href="."is not as predictable anymore, both Firefox and Chrome might have changed how they handle these. I wouldn't rely entirely on my original answer, but rather try both the empty string and the period in different browsers for your specific use and make sure you get the desired behaviour.

使用行为似乎href="."不再那么可预测了,Firefox 和 Chrome 可能已经改变了他们处理这些的方式。我不会完全依赖我的原始答案,而是针对您的特定用途在不同的浏览器中尝试空字符串和句点,并确保您获得所需的行为。

回答by Simon Molloy

None of the other answers will preseve any querystring values. Try

其他答案都不会保留任何查询字符串值。尝试

<a href="javascript:window.location.href=window.location.href">

Admittedly this does involve javascript but, unless your users have script disabled, this is pretty straightforward.

诚然,这确实涉及 javascript,但是,除非您的用户禁用了脚本,否则这非常简单。

回答by Vinay Sahni

One way using javascript:

一种使用javascript的方法:

<a href="javascript:window.location.reload(true)">Reload</a>

回答by Stephen

You could do this: <a href="">This page</a>

你可以这样做: <a href="">This page</a>

but I don't think it preserves GET and POST data.

但我认为它不会保留 GET 和 POST 数据。

回答by Hatzegopteryx

<a href="<?php echo $_SERVER["REQUEST_URI"]; ?>">Click me</a>

回答by kaushik

use this for reload / refresh current page

使用它来重新加载/刷新当前页面

<a href="#" onclick="window.location.reload(true);">

回答by Abie Abby Del Ray

<a href=".">refresh current page</a>

or if you want to pass parameters:

或者如果你想传递参数:

<a href=".?curreny='usd'">refresh current page</a>

回答by Thomas Shields

<a href="/">Clicking me refreshes the page</a>

<a href="/">Clicking me refreshes the page</a>

<a href="?">Click Me To Reload the page</a>

<a href="?">Click Me To Reload the page</a>

回答by Michael Walsh

There is no global way of doing this unfortunately with only HTML. You can try doing <a href="">test</a>however it only works in some browsers.

不幸的是,仅使用 HTML 没有全局方法可以做到这一点。您可以尝试这样做,<a href="">test</a>但它仅适用于某些浏览器。

回答by John Henckel

Completely idempotent url that preserves path, parameters, and anchor.

保留路径、参数和锚点的完全幂等 url。

 <a href="javascript:"> click me </a>

it only uses a little tiny bit of JS.

它只使用了一点点 JS。

EDIT:this does not reload the page. It is a link that does nothing.

编辑:这不会重新加载页面。这是一个什么都不做的链接。