Html 链接到没有查询字符串的当前页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1507225/
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
Link to current page without query string
提问by Alex S
I know there are a lot of tricks for doing links, for example <a href="?query=string">
will link to the current page after appending the query string. Is there a way to link back to the current page, after removing the query string without just typing the file name?
我知道做链接有很多技巧,例如<a href="?query=string">
在附加查询字符串后链接到当前页面。在删除查询字符串而不只是输入文件名后,有没有办法链接回当前页面?
Example, at the page foo.php?q=3
, I want to link to foo.php
. Is there a shortcut-type way to do this? The file will be renamed several times, so I don't want to type a bunch of links and then have to edit them later.
例如,在页面上foo.php?q=3
,我想链接到foo.php
. 有没有捷径类型的方法来做到这一点?该文件将被重命名几次,所以我不想输入一堆链接,然后必须在以后编辑它们。
Edit: Even though these are PHP files, I'm trying to avoid a server-side solution for this particular problem.
编辑:尽管这些是 PHP 文件,但我试图避免针对此特定问题使用服务器端解决方案。
回答by Alex Barrett
href="?"
Not exactlywhat you are after - there's still a question mark at the end - but functionally equivalent.
不完全是你所追求的——最后仍然有一个问号——但在功能上是等效的。
回答by Markus Amalthea Magnuson
回答by Micha?l van de Weerd
I know it's almost 10 years later, but another way to do this client-side is to use Javascript, e.g.
我知道已经快 10 年了,但是在客户端执行此操作的另一种方法是使用 Javascript,例如
<a href="javascript:window.location.href=/^[^?]+/.exec(window.location.href)[0]">Anchor</a>
Not very pretty, but it does result in a clean URL when clicked.
不是很漂亮,但它确实会在点击时产生一个干净的 URL。
回答by Anthony
Are you wanting a client-side equivalent to $_SERVER['PHP_SELF']
? Something that will always link to the file with no extra stuff after the extension?
你想要一个相当于 的客户端$_SERVER['PHP_SELF']
吗?在扩展后总是链接到文件而没有额外内容的东西?
I'm not sure if this works in all browsers, but it works great in Firefox 3.5:
我不确定这是否适用于所有浏览器,但它适用于 Firefox 3.5:
<a href="">Foo</a>
回答by Tim
You can link to the current page you're on with:
您可以链接到当前所在的页面:
<a href="#">blah</a>