How to link a webpage to itself without first knowing its name?

14,632

Solution 1

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?

Solution 2

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

Solution 3

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.


Here is a short list of common hyperlinks:

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 😅

Share:
14,632
Pacerier
Author by

Pacerier

# 9

Updated on July 26, 2022

Comments

  • Pacerier
    Pacerier almost 2 years

    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.

  • JJJ
    JJJ about 11 years
    This will only jump to the top of the page. It doesn't trigger a page refresh.
  • rlwheeler
    rlwheeler about 11 years
    read this bold have you tried using the full url? <a href=fullurl.com/yourpage.html>refresh</a>
  • JJJ
    JJJ about 11 years
    . would drop the file name (example.com/foo/bar.html would become example.com/foo/).
  • Bart
    Bart about 11 years
    Ah ok. Changed my answer to it's original form ;_)
  • Pacerier
    Pacerier about 11 years
    @Bart, to us it's just a question mark. to the server this is a completely different page.
  • Bart
    Bart about 11 years
    @Pacerier - That should not be the case. And if it is. The server config is faulty. The ? denotes the start of a query string and not a location.
  • Pacerier
    Pacerier about 11 years
    @Bart, unlike #, ? is a part of the URL (per RFC). It's presence modifies the URL. The server sees it as two different URLs, although it may return the same resource if it wants to.
  • Bart
    Bart about 11 years
    If so. Why would it be a problem? I'm curious to know.
  • JJJ
    JJJ about 11 years
    Did you read the question? The OP specifically doesn't want to hard-code the full url in.
  • Pacerier
    Pacerier almost 10 years
    This doesn't work. Try it. It links to the top page http://example.com/ and not to the current page (unless the current page is also the top page)
  • Pacerier
    Pacerier almost 10 years
    This doesn't work. Try it. It links to the top page http://example.com/ and not to the current page (unless the current page is also the top page)
  • Pacerier
    Pacerier almost 10 years
    Meta refresh also requires us to state the URL explicitly... Yes, relying on JavaScript will definitely do the trick (since we can read window.location), but I'm wondering if there exists a non-JS solution.
  • Pacerier
    Pacerier almost 10 years
    Yes, relying on JavaScript will definitely do the trick (since we can read window.location), but I'm wondering if there exists a non-JS solution.
  • Pacerier
    Pacerier almost 10 years
    @Bart, because even though your server may return the same resource, the server I'm on isn't configured to do so. It will break my page in many ways, including spoil caching etc.
  • Bart
    Bart almost 10 years
    @Pacerier True. Thank you for clarifying. I learned a lot in the meanwhile, so I don't really agree with my own comments stated above anymore :-)