Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 916
Anand Sainath Avatar asked Nov 12 '22 13:11

Anand Sainath


1 Answers

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.

like image 152
David Mulder Avatar answered Nov 15 '22 06:11

David Mulder