ios - Javascript stops executing in iframe after back button is pressed in mobile safari - Stack Overflow

I have a web page intended for mobile phones that includes an iframe with links that open in the parent

I have a web page intended for mobile phones that includes an iframe with links that open in the parent target.

In Safari on iOS 5.0.1, when a user clicks a link, then uses the back button to go back to the page, javascript stops executing within the iframe.

A simple demo to illustrate the problem:

Click the link, confirm the alert and use the back button. The second time you click the link the alert won't show.

index.html:

<html>
  <body>
    <iframe src="iframe.html"></iframe>
  </body>
</html>

iframe.html:

<html>
  <body>
    <a target="_parent" onclick="alert('Click')" href="">
      Link
    </a>
  </body>
</html>

I'm running out of ideas of what could be causing this. Has anyone run in to this?

I have a web page intended for mobile phones that includes an iframe with links that open in the parent target.

In Safari on iOS 5.0.1, when a user clicks a link, then uses the back button to go back to the page, javascript stops executing within the iframe.

A simple demo to illustrate the problem:

Click the link, confirm the alert and use the back button. The second time you click the link the alert won't show.

index.html:

<html>
  <body>
    <iframe src="iframe.html"></iframe>
  </body>
</html>

iframe.html:

<html>
  <body>
    <a target="_parent" onclick="alert('Click')" href="http://www.google.">
      Link
    </a>
  </body>
</html>

I'm running out of ideas of what could be causing this. Has anyone run in to this?

Share Improve this question edited Feb 1, 2012 at 10:50 mikgra asked Feb 1, 2012 at 10:39 mikgramikgra 711 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I have ran into the same issue. It seems that iPad's Safari snapshots the page and when you go back to it using the back button, it loads that snapshot and javascript code is not being executed.

It seems to be related to this issue: http://www.mac-forums./forums/internet-networking-wireless/257631-safari-ipad-back-button-generates-old-info.html

Very annoying. I haven't found a solution yet.

It's a Webkit bug: Location and other objects are dysfunctional after a document gets restored from [back/forward] cache

Adding an empty unload handler seems to avoid the problem in iOS 4 but not iOS 5.

check out the answer to Problems with Page Cache in iOS 5 Safari when navigating back / unload event not fired.

The answer that worked for me was to do the following:

<body onunload="">
...
<script type="text/javascript">
if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) {
  window.onpageshow = function(evt) {
    // If persisted then it is in the page cache, force a reload of the page.
    if (evt.persisted) {
       document.body.style.display = "none";
       location.reload();
    }
  };
}
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744236507a4564496.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信