1

I have a simple piece of code that if a user clicks cancel, then they are returned to the previous page using the attached code. What I would like to do is, if a user clicks the cancel button, then refresh the page they came from. Is this possible? Many thanks

<input type="button" value="Cancel" onclick="history.go(-1);return false;" />
Celeritas
  • 14,489
  • 36
  • 113
  • 194
user1532468
  • 1,723
  • 8
  • 41
  • 80
  • What's the difference? If they click refresh they are returned to the previous page but you want it to "refresh the page they came from" isn't that the same? I'm not sure you are using the word refresh correctly. – Celeritas Jul 24 '12 at 07:34
  • 1
    @Celeritas it is not exactly the same, because browsers mostly load pages from cache and not from server, of course it depends on user settings, but by default they load from cache – haynar Jul 24 '12 at 07:40
  • Hi. Not it's not the same. When they return, all the old data is still present. That is why I need to refresh. Thanks – user1532468 Jul 24 '12 at 07:41

3 Answers3

1

window.parent.document.location.reload(); i think may work with jQuery. Set up a bind for the back button.

or throw this

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

in the head of the first page so it expires and will auto refresh

however I am not 100% sure that will work with firefox. It seems to work with IE.

I think you could also open a page without AJAX by making the rel = external

<a href="../index.html" data-icon="grid"
class="ui-btn-right" rel="external">Home</a>

that an old school way to do it.

Also This is a very useful link explaining how jquery can accomplish this in most browsers. Probaly a more optimal solution rather then my hacks.

Community
  • 1
  • 1
Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71
  • `window.parent.document.location.reload();` is not the solution because here the operation takes place in the same window and when you're going back in history the window object is the same so you can't access to the window object of previously opened page, anyway +1 for `meta` tags – haynar Jul 24 '12 at 07:45
  • but who said that he uses AJAX? I will say he definitely doesn't use AJAX as he speaks about `history.go(-1)` – haynar Jul 24 '12 at 07:47
  • @Bob. The meta code did not work. Also, there is no AJAX. Thanks – user1532468 Jul 24 '12 at 08:39
  • which browser are you trying to get it to work with. you put that meta code in your html right? – Frank Visaggio Jul 24 '12 at 08:40
  • @Bob. I have tried firefox,chrome,ie8. I put the code in the head section.Thanks – user1532468 Jul 24 '12 at 08:49
1

you can either use meta tags suggested in previous answer or check the document.referrer property against a specific URL and than refresh it manually via javascript.

edit

in 1.html you will have:

$(document).ready(function(){
    if(document.referrer.indexOf("files.php") != -1) {
        window.location.reload(true); // passing true as an argument causes page to being force loaded from the server
    }
});

and in the 2.html you just need this:

<input type="button" value="Cancel" onclick="history.go(-1);return false;" />
haynar
  • 5,961
  • 7
  • 33
  • 53
  • I am not sure I have coded this correctly as it is not working. When the button is clicked, nothing happens. Thanks – user1532468 Jul 24 '12 at 09:35
  • you should place this not on the button click, but in the document.load event of page what you want to reload, if you use jQuery, this will look like: `$(document).ready(function(){ // and the code from above });` – haynar Jul 24 '12 at 09:38
  • also use the whole URL when comparing, not only the part `files.php`, or you can if the document.referrer contains or ends with `files.php` but not equals, because it will return an absolute URL – haynar Jul 24 '12 at 09:39
  • I am not using Jquery. So i put it in function and just called it the onclick event: GoBack(); Still nothing. – user1532468 Jul 24 '12 at 09:45
  • if you don't use jQuery, then take a look at this post: http://stackoverflow.com/questions/1235985/attach-a-body-onload-event-with-js and add an event listernet to the first page – haynar Jul 24 '12 at 09:55
1

Another possibility is if you're only concerned with reloading pages from your website you could force no caching. If you're interested see here.

Community
  • 1
  • 1
Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • Even tried: response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. response.setHeader("Pragma", "no-cache"); // HTTP 1.0. response.setDateHeader("Expires", 0); // Proxies. Still not work. – user1532468 Jul 24 '12 at 08:44
  • You tried it on the page they were coming from right? Not the page that would be sending them back. I'm surprised it didn't work but if you've got it working a different way never mind. – Celeritas Jul 24 '12 at 18:03