Coder Perfect

Reload the current page using this link

Problem

Is it feasible to create a regular link to the present location?

I’ve found two ways so far, however one of them uses JavaScript and the other requires you to know the page’s absolute URL:

<a href="#" onclick="window.location.reload(true);">1</a>
<a href="/foobar/">2</a>
<a href="#">3 (of course not working)</a>

Is there a way to achieve this without having to know the absolute path or using JavaScript?

Asked by Tyilo

Solution #1

I’ve been utilizing:

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

I have yet to come across a circumstance and/or browser where it does not function properly.

The current course is referred to as the period. If you have this file structure, you can also use.. to refer to the folder above the current path:

page1.html
folder1
    page2.html

Then, in page2.html, you can write:

<a href="../page1.html">link to page 1</a>

EDIT:

I’m not sure if this is a new behavior or if it has always been this way, but Chrome (and possibly others) will treat periods in the manner indicated above as referring to directories rather than files. This means that if you go to http://example.com/foo/bar.html, you’re actually in the /foo/ directory, and a href value of. in bar.html refers to /foo/ rather than bar.html.

Consider it like traversing a terminal’s file system; you can never cd into a file:)

EDIT 2:

Both Firefox and Chrome appear to have modified how they treat href=”.” It appears that the behavior of using href=”.” is no longer as predictable. I wouldn’t rely solely on my original response; instead, for your specific use, try both the empty string and the period in multiple browsers to ensure you obtain the required result.

Answered by Markus Amalthea Magnuson

Solution #2

None of the other options will set any querystring values for you. Try

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

Although this does require javascript, it is rather simple unless your users have script disabled.

Answered by Simon Molloy

Solution #3

Using JavaScript in one way:

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

Answered by Vinay Sahni

Solution #4

You may try this: a href=””>a href=””>a href=””>a href=”” /a>This page

However, I don’t believe it saves GET and POST data.

Answered by Stephen

Solution #5

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

Answered by Hatzegopteryx

Post is based on https://stackoverflow.com/questions/8174282/link-to-reload-current-page