5

我有一个发布内容的页面。当我使用 Cmd+R 或使用浏览器刷新按钮在 Safari 中刷新所述页面时,会出现如下弹出窗口: 在此处输入图像描述

这种行为在其他浏览器(Chrome、FF 等)中也是相同的。

但是,当我尝试在 Safari 中使用window.location.href = location.hreflocation.reload()window.location.reload()甚至window.location.reload(true)来刷新同一页面时,它没有显示上述弹出窗口。但是这种行为在出现相同弹出窗口的 Chrome、FF 等中有所不同。

编辑:不显示弹出窗口的后果是发布的内容不会被重新发送,因此行为会发生变化。

所以我现在的问题是如何在所有浏览器中重新加载页面,以便重新发送发布的表单内容。

4

2 回答 2

1

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.

于 2015-05-28T12:36:11.600 回答
0

如果你真的想在没有确认的情况下使用刷新页面,你应该使用重定向。因此,您将数据发布到页面 A,并将发布信息保存在会话或 cookie 上,然后将页面 B 重定向到呈现。

或者您可以在页面 A 上为您的业务逻辑做所有事情,并使用页面 B 作为感谢页面。

然后,您可以在页面 B 上刷新而无需确认。

于 2015-05-29T03:41:12.933 回答