Anand Sainath
Anand Sainath

Reputation: 1807

Page Reload behaviors in Safari

I have a page that has posted content. When I refresh said page in Safari by using either Cmd+R or using the browser refresh button, a pop-up shows up like below: enter image description here

This behavior is also the same across other browsers (Chrome, FF etc).

But when I try to refresh the same page using javascript by using either window.location.href = location.href or location.reload() or window.location.reload() or even window.location.reload(true) in Safari, it doesn't show the above mentioned pop-up. But this behavior is different in Chrome, FF etc where the same pop-up shows up.

Edit: The consequence of the pop-up not being displayed is that the posted contents do not get resent and thus the behavior changes.

So my question now is How do I reload the page across all browsers so that the posted form contents get re-sent.

Upvotes: 5

Views: 3265

Answers (2)

sean
sean

Reputation: 1682

If you really want to use refresh the page without confirmation, you should use redirect. So you post data to page A, and save the post info on session or cookie, and then redirect the page B to render.

Or you can do everything for your business logic on page A, and use page B as just kind of thank you page.

Then, you can refresh without confirmation on page B.

Upvotes: 0

David Mulder
David Mulder

Reputation: 26990

Through AJAX

As far as I am aware there is no way to force Safari to resend the POST data during a refresh. The only way to 'fix' this issue is to separate the logic from the document in which the result is displayed. What I mean by this in practical terms is that you could trigger the logic you want from code using an AJAX request, whilst the document itself will be a (relatively) dumb page.

So what you would get is:

POST document.ext

#In whatever server side language you're using, the reason this can't be done
# purely in javascript is that POST data is only available to the server.
print "<script>"
print "POSTdata = " + JSONEscape(POSTdata) + ";"
print "var doAction = function(){"
print "  ajax('action.ext', POSTdata, function(result){});"
print "});"
print "</script>"
print "<button onclick='doAction()'>Refresh</button>"

POST action.ext

# all the logic you previously had in document.xxx

The result of this is:

  • In all browsers a user initiated refresh will send the POST data and the logic is re-triggered.
  • The refresh button will at all times re-trigger your logic directly without reloading the entire page.

Self-submitting form

It just hit me that another - easier to implement - way to achieve this is to simply create a <form action='?' method='POST'> and in code trigger it's submit function. You would still need to print the POSTdata, but instead of trigger an Ajax call, you would simply trigger a redirect to the same page through the hidden <form>. The advantage of this is that it doesn't require any restructuring, though I personally think the AJAX method would look neater. A quick Google-fu showed these answers detailing the procress.

Upvotes: 1

Related Questions