Tyilo
Tyilo

Reputation: 30102

Link to reload current page

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?

Upvotes: 256

Views: 480413

Answers (24)

Abie Abby Del Ray
Abie Abby Del Ray

Reputation: 99

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

or if you want to pass parameters:

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

Upvotes: 9

Attic V
Attic V

Reputation: 38

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

<a href > Link </a>

Upvotes: 2

zupa
zupa

Reputation: 13442

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.

Upvotes: 1

Hatzegopteryx
Hatzegopteryx

Reputation: 700

Here is a method that works in PHP:

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

Upvotes: 20

shashank10456
shashank10456

Reputation: 41

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>

Upvotes: 4

Vinay Sahni
Vinay Sahni

Reputation: 5053

One way using JavaScript:

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

Upvotes: 123

kaushik
kaushik

Reputation: 933

use this for reload / refresh current page

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

or use

<a href="">refresh</a>

Upvotes: 15

Ayush Kumar
Ayush Kumar

Reputation: 11

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)

Upvotes: 0

Eternal_
Eternal_

Reputation: 103

<a href="">Refresh!</a>

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

Upvotes: 9

Markus Amalthea Magnuson
Markus Amalthea Magnuson

Reputation: 8741

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.

Upvotes: 250

Love Kumar
Love Kumar

Reputation: 1124

try This

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

Upvotes: 3

Andrew
Andrew

Reputation: 20081

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>

Upvotes: -3

ciccio
ciccio

Reputation: 19

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

Upvotes: -4

John Henckel
John Henckel

Reputation: 11357

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.

Upvotes: 5

Tim Bray
Tim Bray

Reputation: 1462

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.

Upvotes: 0

Pramodh
Pramodh

Reputation: 774

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

Upvotes: -2

SashaPinsk
SashaPinsk

Reputation: 162

One more solution

<a href="javascript:history.go(0)">Reload</a>

Upvotes: 2

SSpoke
SSpoke

Reputation: 5836

<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://

Upvotes: -1

Mark
Mark

Reputation: 599

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

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

Upvotes: 3

david villa
david villa

Reputation: 384

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>

Upvotes: 0

Simon Molloy
Simon Molloy

Reputation: 1864

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.

Upvotes: 167

Michael Walsh
Michael Walsh

Reputation: 122

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.

Upvotes: 6

Thomas Shields
Thomas Shields

Reputation: 8942

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

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

Upvotes: 8

Stephen
Stephen

Reputation: 8178

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

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

Upvotes: 45

Related Questions