53

After calling history.pushState in Safari on iOS, it's no longer possible to use alert(), confirm() or prompt(), when using the browser back button to change back.

Is this an iOS bug? Are there any known workarounds?

Simple example to reproduce this behavior:

<html>
  <body>
    <ul>
      <li>Step 1: <button onclick="alert(Math.random())">Confirm Alert is working</button></li>
      <li>Step 2: <button onclick="history.pushState(null, null, '/debug/'+Math.random());">Change History</button></li>
      <li>Step 3: use your browser back button, to go back</li>
      <li>Step 4: <button onclick="alert(Math.random())">Alert is not working anymore</button></li>
    </ul>
  </body>
</html>

You can try it online here: goo.gl/faFW6o.

Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
dan
  • 5,377
  • 13
  • 39
  • 44
  • Seems to be working here: http://plnkr.co/edit/7MTgwGZWT0mXCSDBEp2b?p=preview – idream1nC0de Jun 28 '16 at 18:34
  • 1
    @JacobHeater unfortunately its not when running standalone (without an iframe) – dan Jun 28 '16 at 18:41
  • 1
    @JacobHeater I've added a live demo to the description. Please feel free to try it out over there. – dan Jun 28 '16 at 18:51
  • It's working for me using the link your provided. – idream1nC0de Jun 28 '16 at 18:53
  • 1
    Are you testing on Safari iOS? – dan Jun 28 '16 at 18:54
  • My apologies. I didn't read that little caveat. – idream1nC0de Jun 28 '16 at 18:57
  • Same issue! I don't know what to do. – Kenny Jan 21 '17 at 21:37
  • I'm experiencing the same problem on iOS. I can find very little via web searching. Here's someone else with the same problem: https://forums.developer.apple.com/thread/65054#198030 – AlpineCarver May 27 '17 at 21:35
  • Seeing the same thing even on iOS 12.0.1. Seems like a bug indeed. Have you filed a radar with apple (bugreport.apple.com) about this? – tmm1 Oct 09 '18 at 21:40
  • 2
    I opened rdar://45141145 with the reproduction steps above. They asked me to submit a sysdiagnose and a video of the issue as well, which I have just done. – tmm1 Oct 15 '18 at 21:40
  • "Engineering has determined that your bug report (45141145) is a duplicate of 25868851 and will be closed." – tmm1 Oct 19 '18 at 15:54
  • @tmm1 as rdar is only for Apple engineers, I can't see 25868851. Could you please share the progress of that radar? – ZeroCho Feb 11 '19 at 08:52
  • I cannot see that radar either, because I didn't create it. All I can see is my radar, which was closed because it is duplicate. – tmm1 Feb 11 '19 at 18:04
  • 2
    Here are my findings, once you use ios safari back button, I think browser loading content from cache, therefore js is not working. Instead of using back button using window.history.go(-1) works. I tried to detect back buttons but I've failed. Additionally tried this but it didn't refresh. Maybe I'm missing something window.onpageshow = function(event) { if (event.persisted) { window.location.reload() } }; – siniradam Sep 02 '19 at 21:29
  • works for me so it does –  Nov 12 '19 at 14:11
  • 4
    Still a bug in 2022. – izaguirrejoe Mar 22 '22 at 14:13

2 Answers2

1

This is because of the back-forward cache in Safari.

You can use the following code to force a reload when the back-button is pressed.

window.onpageshow = function(e) { // e -> event
    if (e.persisted) {
        window.location.reload(); 
    }
};

Additionally, if you are using jQuery ...

$(window).bind("pageshow", function(e) { // e -> event
    if (e.originalEvent.persisted) {
        window.location.reload();
    }
});
0

one workaround would be instead the property onload on the button, create a function adding the listeners then call it on window.onpopstate and window.onload..