How to refresh the page by clicking the back button?

I use .htaccess to route all my traffic to a single index.php file. The code below directs traffic, but for some reason it does not work with the back button. I don’t know what to do to make the return button work. I tried different things and looked for a lot, and none of them worked, so I hope someone here can help!

<?php if(isset($_GET['parameters'])) { if($_GET['parameters'] == "repair") include 'repair.html'; else if($_GET['parameters'] == "training") include 'training.html'; else if($_GET['parameters'] == "products") include 'products.html'; else if($_GET['parameters'] == "about") include 'about.html'; else if($_GET['parameters'] == "employees") include 'employees.html'; else if($_GET['parameters'] == "training") include 'training.html'; else if($_GET['parameters'] == "newaccount") include 'newaccount.html'; else if($_GET['parameters'] == "contact") include 'contact.html'; else if($_GET['parameters'] == "recommended") include 'recommended.html'; else if($_GET['parameters'] == "careers") include 'careers.html'; else include 'home.html'; } else include 'home.html'; ?> 

So far I have tried and not used: window.onbeforeunload body onunload=""

There must be a way onunload=location.reload() or something like that! For some reason you need to know the syntax !!

+23
javascript jquery ajax php .htaccess
Jan 03 '14 at 8:11
source share
9 answers

Try this ... not verified. Hope this works for you.

Create a new php file. You can use the back and forward buttons, and the number / time stamp on the page is always updated.

 <?php header("Cache-Control: no-store, must-revalidate, max-age=0"); header("Pragma: no-cache"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); echo time(); ?> <a href="http://google.com">aaaaaaaaaaaaa</a> 

Or

another solution found

The onload event should be fired when the user clicks the back button. Elements not created using JavaScript retain their values. I suggest that you back up the data used in the dynamically created element to INPUT TYPE = "hidden", which will be displayed: no then onload, using the input value to rebuild the dynamic elements as they were.

 <input type="hidden" id="refreshed" value="no"> <script type="text/javascript"> onload=function(){ var e=document.getElementById("refreshed"); if(e.value=="no")e.value="yes"; else{e.value="no";location.reload();} } 
+43
Jan 03 '14 at 8:21
source share

A later solution uses the PerformanceNavigation interface :

 if(!!window.performance && window.performance.navigation.type === 2) { console.log('Reloading'); window.location.reload(); } 

If the value 2 means "Access to the page by navigating through the history."

View browser support here: http://caniuse.com/#search=Navigation%20Timing%20API

Reload the site when it is reached using the browser button back

+32
Apr 13 '17 at 18:48
source share

The hidden input solution did not work for me in Safari. The solution below works and came out of here .

 window.onpageshow = function(event) { if (event.persisted) { window.location.reload() } }; 
+13
Jan 18 '16 at 2:56 on
source share

I found two ways to handle this. Choose the best for your business. Solutions Tested on Firefox 53 and Safari 10.1

1. Detect if the user uses the back / forward button, and then reload the whole page

 if (!!window.performance && window.performance.navigation.type === 2) { // value 2 means "The page was accessed by navigating into the history" console.log('Reloading'); window.location.reload(); // reload whole page } 

2. reload the whole page if the page is cached

 window.onpageshow = function (event) { if (event.persisted) { window.location.reload(); } }; 
+9
Jun 08 '17 at 12:43 on
source share

This is a simple solution, posted here to force refresh the page on the back button ...

I tried to make the page load again by the browser when the user clicks the back button, but nothing works. It seems that modern browsers have a separate cache for pages, which stores the full state of the page (including DOM elements created using JavaScript), so when users click the "Back" button, the previous page is displayed instantly in the state when the user has it left. If you want to force the browser to reload the page using the "Back" button, add onunload = "" to your HTML (X) body element:

<body onunload="">

This disables the special cache and forces the page to reload when the user presses the Back button. Think twice before using it. The fact that such a solution is necessary is a hint that your concept of site navigation is wrong.

+8
May 20 '15 at 9:21
source share

Have you tried something like this? not verified...

 $(document).ready(function(){ $('.ajaxAnchor').on('click', function (event){ event.preventDefault(); var url = $(this).attr('href'); $.get(url, function(data) { $('section.center').html(data); var shortened = url.substring(0,url.length - 5); window.location.hash = shortened; }); }); }); 
+2
Jan 03 '14 at 8:26
source share

First of all, paste the code into your code:

 <input id="reloadValue" type="hidden" name="reloadValue" value="" /> 

then run jQuery:

 <script type="text/javascript"> jQuery(document).ready(function() { var d = new Date(); d = d.getTime(); if (jQuery('#reloadValue').val().length === 0) { jQuery('#reloadValue').val(d); jQuery('body').show(); } else { jQuery('#reloadValue').val(''); location.reload(); } }); 

+1
May 03 '16 at 12:47
source share
window.onbeforeunload = function () {redirect (window.history.back (1)); };
0
Jun 09 '16 at 14:52
source share

Ahem u_u

As I said, the return button for each of us is a pain in some place ... it says ...

While you are loading the page, it causes a lot of problems ... for a standard "site" it will not change so much ... however, I think you can do something like this

Each time a user accesses your .php page, which chooses what to load. You can work a little with the cache (so as not to cache the page) and, possibly, the expiration date.

But for a long-term solution, code will be added in the onload event to get Ajax data, so you can (with Javascript) run the code you need and an example to refresh the page.

-four
Jan 03 '14 at 8:21
source share



All Articles