Reputation: 89703
To link a page to itself (e.g. http://example.com/folder/ThisPage.html), we can simply create a href as such:
ThisPage.html:
<a href="ThisPage.html">Link</a>
This works, but has the disadvantage of needing to be updated when the file name changes. For example, if the file name changes to ThatPage.html
, our href needs to change accordingly to <a href="ThatPage.html">Link</a>
.
I'm looking for an alternative without that disadvantage. I've tried:
<a href="?">Link</a>
Doesn't work as <a href="ThisPage.html">Link</a>
does, because it appends a "blank query part" (question mark) to the URL.
<a href="">Link</a>
Doesn't work as <a href="ThisPage.html">Link</a>
does, on some browsers (e.g. Opera).
How do we link a page to itself, without having to update the relevant portion when the name of the page changes?
Note: JavaScript not allowed.
Upvotes: 9
Views: 20900
Reputation: 51
It's very simple, just leave the href=""
blank. So that's how:
<a href="">Click me to refresh page</a>
But this is not necessarily a good idea, because the cache may not be cleared, and whatever you need it for, if the page has changed in the meantime the change may not appear despite the reload. Probably a better idea is the javascript code location.reload();
to take. But there are enough explanations on other sites, which is why I won't explain it here. You can of course also for example take a question mark (?), but this is unnecessary, actually not intended for it and can cause problems depending on the program.
Points to the root page
<a href="/">Link</a>
Points to a file relative to the root page
<a href="/file.ext">Link</a>
Points to a file relative to the current file
<a href="file.ext">Link</a>
Points to a file in the previous folder
<a href="../file.ext">Link</a>
Points to a file in the second previous folder
<a href="../../file.ext">Link</a>
Points to a file in a folder below
<a href="folder/file.ext">Link</a>
Points to the current file
<a href="">Link</a>
Points to a page with a different host but the same protocol
<a href="//domain.tld/file.ext">Link</a>
I hope that my answer will help some people, because I found it via a search engine and saw that there is no correct answer. And it's my first answer here 😅
Edit (after two and a half years):
I just searched for the official specifications and this is what I found:
Same-document References
“A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, […]”
That means, as I sad, this kind of hyperlink doesn’t necessarily reload the page. As a pure HTML solution, this is not possible.
https://datatracker.ietf.org/doc/html/rfc2396#section-4.2
Upvotes: 5
Reputation: 428
Why not try <a href="./"></a>
?
I looked some things up, and as it turns out, ./
refers to current directory.
Upvotes: 0
Reputation: 17371
Just use <a href="?">Link</a>
. Nobody cares about the question mark appended to the URL. It does the requirement and that is what counts right?
Upvotes: 6
Reputation: 1250
Just do this:
<a href = "/">This Very Site</a>
Source: I saw this in the source code of Matthew Alger's website. Check it out for yourself!
Upvotes: 0
Reputation: 719
If you want to reload the page, you really should take a look into javascript. It is the best way to do it.
Upvotes: 0
Reputation: 546
Here ya go. Hope this is what you are looking for
<a href="#">Link</a>
Upvotes: -3
Reputation: 6286
If you want it to go nowhere, you can use
<a href="javascript:;">link</a>
But if you want it to reload the page, you'll have to go with JavaScript.
If you want to reload the page you could use the Meta refresh tag
http://www.w3.org/TR/WCAG20-TECHS/H76.html
Upvotes: 1