238

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:

<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?

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
Tyilo
  • 28,998
  • 40
  • 113
  • 198

24 Answers24

240

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:

page1.html
folder1
    page2.html

You can then in page2.html write:

<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.html you are really in the directory /foo/ and a href value of . in bar.html will refer to /foo/ rather than bar.html

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

EDIT 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.

radbyx
  • 9,352
  • 21
  • 84
  • 127
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
  • 29
    It should be noted though that GET data is not preserved with this method, consider it a "hard reload". Using an empty href value will preserve GET data, but clear hash value (like #these). Using # or ? as href value will add "garbage" to the new URL. I've found that using period is the cleanest way to achieve a reload. – Markus Amalthea Magnuson Aug 24 '12 at 11:43
  • 1
    Markus is right - for example in a CakePHP app where URL is /controller/method/ID format (e.g. /things/edit/26 ) then this would load as /things/edit ). Nice technique anyway! – Luke Barker Jan 29 '13 at 12:14
  • 6
    This technique isn't working for me on mac chrome. Though using "." as anhor, it behaves as if ".." and links to the parent page. e.g. /admin/stuff becomes /admin. Anyone else seeing this behaviour? – xnzac Oct 21 '13 at 09:22
  • 5
    This solutions is wrong. Also in Chrome doesn't work and point to parent directory. You should use empty href as @MarkusAmaltheaMagnuson suggest – coorasse Dec 10 '13 at 09:30
  • I've added a note on the Chrome behavior, which in fact is logically correct after all. The single period technique is still useful if your URL's are structured with trailing slashes. – Markus Amalthea Magnuson Dec 10 '13 at 14:04
  • 12
    This seems to only work if the path component of the URL ends with a `/`. – Kos Apr 16 '14 at 13:25
  • This answer will also not keep the fragment, the part of the URL after the "#". This is increasingly important today with Angular and other robust client-side web pages. – Patrick Szalapski Aug 09 '16 at 15:53
  • 8
    As of today, this doesn't work in Firefox or Chrome. Both browsers reference the parent directory (not the current page) using href="." – jtubre Jun 05 '17 at 17:16
  • Use href=? if you *don't* want params on reload, per: https://stackoverflow.com/a/1507247/1483861 – ognockocaten Jan 31 '19 at 19:21
  • 1
    It looks like at least in Firefox having an empty string (i.e. `href=""`) refers to the current page's location. – rsethc Feb 20 '19 at 07:24
  • 1
    @MarkusAmaltheaMagnuson -Doesn't an empty string do the same thing? – ashleedawg Apr 27 '19 at 09:18
  • I'm not sure, I think behaviour might have changed in browsers since I originally posted this, it could be that the effect is the same in some browsers and not others. If you rely on JavaScript for other things, I think the safest is to use JS to reload the page. If, as the original poster wanted, you do not want to rely on JS, testing both `.` and the empty string in several browsers and deciding yourself is the way to go. – Markus Amalthea Magnuson Apr 27 '19 at 14:19
158

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.

Simon Molloy
  • 1,774
  • 1
  • 11
  • 13
116

One way using JavaScript:

<a href="javascript:window.location.reload(true)">Reload</a>
informatik01
  • 16,038
  • 10
  • 74
  • 104
Vinay Sahni
  • 4,873
  • 3
  • 23
  • 17
  • 6
    Preserves querystrings and anchors (`?query=string#anchor`). Great! – analog-nico Jun 22 '16 at 15:02
  • 1
    I ended up using this answer because the scroll position is also retained. Useful if you are using a reload as a pseudo "undo" button. – igneosaur Mar 21 '17 at 20:38
  • This sollution WORKS on React client – Andrius May 31 '17 at 11:10
  • could you please elaborate on how to use this in html syntax? – JoaMika Apr 18 '19 at 13:21
  • 3
    I have a question, is the `true` parameter really what you should go with for this problem when using the `window.location.reload` method? That will make it a hard refresh that avoids pulling from cache, which may be what you want under some circumstances, but I'd think in most situations you'd want to take advantage of cache, wouldn't you? – Stephen M Irving Jan 21 '20 at 19:37
  • dependable and simple ... I believe this should be the best answer as the '.' solution above does not work, even as stated by the author – John Contarino May 18 '20 at 19:45
  • `Reload` will work without javascript (but scroll to top) as well as with javascript (and keep scroll position). – BenderBoy Feb 27 '21 at 21:41
  • I'm seeing: `Warning: A future version of React will block javascript: URLs as a security precaution. Use event handlers instead if you can. If you need to generate unsafe HTML try using dangerouslySetInnerHTML instead. React was passed "javascript:window.location.reload(true)".` so I used `Link` for React (and defined `reloadPage`). – Sooth Jun 01 '22 at 22:59
  • Good point, Stephen. I'm adding this button in case if app failed to download all required resources, this is usually caused by a weak proxy or bad internet connection, and in this case, I do want to preserve the cache to help them eventually download everything that is needed. – Maxim Mazurok Jun 06 '22 at 07:47
  • Year 2023 update: the `true` parameter is not in the standard specification. Quote from [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Location/reload): `So a boolean parameter is not part of the current specification for location.reload() — and in fact has never been part of any specification for location.reload() ever published.` – hankchiutw Apr 21 '23 at 03:26
43

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

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

Stephen
  • 7,994
  • 9
  • 44
  • 73
  • 3
    Unfortunately, it does not work in IE. From: msdn.microsoft.com/en-us/library/cc848861%28v=vs.85%29.aspx - _If HREF is specified as a blank value (href="" or href=), executing the link might display the directory containing the current document, or it might generate an error, depending on other elements in the document and the server environment._ – Bohdan Lyzanets Oct 02 '13 at 09:46
17

Here is a method that works in PHP:

<a href="<?php echo $_SERVER["REQUEST_URI"]; ?>">Click me</a>
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Hatzegopteryx
  • 600
  • 6
  • 13
  • 5
    Noticed the -1, might not be not the solution in your case (becouse it is a php solution) but i think it is the cleanest solution here. This becouse it gives a valid value to the href attribute, it is the cleanest. If possible avoid javascript. So +1. – rofavadeka Oct 05 '13 at 12:40
  • 2
    ty very helpful. Why this getting thumbs down? The original question does not mention js either and most of q's here reference JS... – Andrew Aug 31 '17 at 18:12
  • 1
    @Andrew the original question is tagged with #html and #hyperlink. While admittedly "a normal link pointing to the current location" is an incredibly vague description, I'm pretty sure it can be reasonably interpreted as seeking a client-side-only solution. An anchor where the href is populated by PHP is not a "normal link" by any stretch, it's PHP code. – endemic Mar 15 '18 at 04:07
  • 2
    @endemic To suggest javascript is "normal" and php is not is a pointless opinion. The OP actually defines normal as not including javascript, and not using an absolute path. Interestingly enough most of the answers are in javascript. – Andrew Mar 15 '18 at 12:59
  • I didn't suggest JavaScript was "normal" (and again, neither am I agreeing to that usage of the word, funny though it is.). I just said that, given the context, PHP is obviously not. (In my inflammatory opinion, if you have access to Python server software, I would never consider using PHP to be "normal" behavior. :P) It seems like a useful answer to a different SO question. – endemic Mar 16 '18 at 06:33
  • I have HTML, I have JS and I have PHP, I don't have Python. Thanks, @pteryx! – brasofilo Jan 23 '21 at 23:40
14

use this for reload / refresh current page

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

or use

<a href="">refresh</a>
Eternal_
  • 73
  • 1
  • 4
kaushik
  • 903
  • 7
  • 16
9
<a href=".">refresh current page</a>

or if you want to pass parameters:

<a href=".?currency='usd'">refresh current page</a>
Mystical
  • 2,505
  • 2
  • 24
  • 43
7

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

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

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
6
<a href="">Refresh!</a>

Leave the href="" blank and it will refresh the page.
Also works if some info is passed using forms.

Azametzin
  • 5,223
  • 12
  • 28
  • 46
Eternal_
  • 73
  • 1
  • 4
6

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.

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
Michael Walsh
  • 122
  • 1
  • 7
  • It does not work in IE. From: msdn.microsoft.com/en-us/library/cc848861%28v=vs.85%29.aspx - _If HREF is specified as a blank value (href="" or href=), executing the link might display the directory containing the current document, or it might generate an error, depending on other elements in the document and the server environment._ – Bohdan Lyzanets Oct 02 '13 at 09:47
  • 1
    I tested and it works in all major modern browsers (Chrome, Firefox, Edge, Opera, Safari) – Donald Duck Aug 28 '19 at 15:41
5

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

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

it only uses a little tiny bit of JS.

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

John Henckel
  • 10,274
  • 3
  • 79
  • 79
  • @FranciscoCorralesMorales, Sorry but I think I misunderstood the question. I thought the OP was trying to make a link that does nothing. But now I see that the OP wants a link that will reload the current page. (I'll edit). – John Henckel Oct 20 '16 at 16:13
  • Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight May 16 '17 at 12:32
4

For me, the answers previously provided here, were opening a new tab/window (probably because of my browser settings). But i wanted to reload/refresh on the same page. So, the above solutions did not work for me.

However, the good news is, the following (either of two) worked for me.

  1. <a onclick="javascript:window.location.reload();"> refresh</a>
  2. <a onclick="window.location.href=this">refresh</a>
3

While the accepted answer didn't work for me in IE9, this did:

<a href="?">link</a>
Mark
  • 551
  • 6
  • 13
  • 1
    I don't think this will work if you're interested in preserving querystring parameters. – WynandB Feb 26 '15 at 05:00
  • 1
    @WynandB is correct. this will trash all GET and POST parameters. if that's what you want, or if you don't care, this trick works great, though. – hanshenrik Jun 17 '17 at 20:16
3

try This

<a href="javascript:window.location.href=window.location.href">
Love Kumar
  • 1,056
  • 9
  • 10
2

One more solution

<a href="javascript:history.go(0)">Reload</a>
SashaPinsk
  • 162
  • 10
  • Good solution! One tiny tip for that method is 0 is the default parameter, so if you are trying to reload, you could just write: `history.go()` with no argument. – Stephen M Irving Jan 21 '20 at 19:35
0

I use JS to show only the div with a specific id in the tags page in a jekyll site. With a set of links in the same page, you show the corresponding element:

function hide_others() {
    $('div.item').hide();
    selected = location.hash.slice(1);
    if (selected) {
        $('#' + selected).show();
    }
    else {
    $('div.item').show();
    }
}

links use links like:

<a href="javascript:window.location.href='/tags.html#{{ tag[0] }}-ref'; hide_others()">{{ tag[0] }}</a>
david villa
  • 384
  • 4
  • 9
0

You can use a form to do a POST to the same URL.

 <form  method="POST" name="refresh" id="refresh">
 <input type="submit" value="Refresh" />
 </form>

This gives you a button that refreshes the current page. It is a bit annoying because if the user presses the browser refresh button, they will get a do you want to resubmit the form message.

Tim Bray
  • 1,373
  • 10
  • 7
0

I AM USING HTML TO SOLVE THIS PROBLEM
Use:

<a href="page1.html"> Link </a>

*page1.html -> type the name of the file you are working on (your current HTML file)

0

Two options:

<a href="https:">link</a>
<a href="">link</a>

In both cases the browser will resolve the link relative to the current URL, resulting in the current URL excluding the #fragment.

Notes:
  • https: if you are on HTTPS protocol (match whatever the protocol is)
  • Having an empty URL might look like a mistake for others, https: may be more explicit.
  • This won't replay a POST request.
zupa
  • 12,809
  • 5
  • 40
  • 39
0

If you don't care about preserving query-string values, then an easy option would be:

<a href > Link </a>
Attic V
  • 16
  • 5
-1
<a href="/">Same domain, just like refresh</a>

Seems to work only if your website is index.html, index.htm or index.php (any default page).

But it seems that . is the same thing and more accepted

<a href=".">Same domain, just like refresh, (more used)</a>

Both work perfect on Chrome when domain is both http:// and https://

SSpoke
  • 5,656
  • 10
  • 72
  • 124
-2

<a href="/home" target="_self">Reload the page</a>

Pramodh
  • 775
  • 2
  • 9
  • 17
-3

If you are using php/smarty templates, u could do something like this:

<a href="{{$smarty.server.REQUEST_URI}}{if $smarty.server.REQUEST_URI|strstr:"?"}&{else}?{/if}newItem=1">Add New Item</a>
Andrew
  • 18,680
  • 13
  • 103
  • 118
-4

Just add target="_blank" and the link will open a new page keeping the original page.

ciccio
  • 19
  • 6
  • As you say, this will just open whatever page is specified in the `href` attribute in a new window/tab. This doesn't in any way answer the actual question. – Philip Stratford Sep 26 '17 at 09:38