你如何禁用前进和后退缓存iOS上的Safari浏览器? [英] How do you disable back and forward caching on iOS Safari?

查看:785
本文介绍了你如何禁用前进和后退缓存iOS上的Safari浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC我们有iOS上的问题,禁用前进和后退缓存。我们不希望任何我们在我们的网站页面是由前进和后退按钮用于安全原因无法访问。我们试过设置:

In ASP.NET MVC we are having problems disabling back and forward caching on iOS. We don't want any of our pages in our site to be accessible from back and forward buttons for security reasons. We've tried setting:

[OutputCache(NoStore = true, Duration = 1)]

和一堆其他的东西,但没有任何工程。我们甚至不能在onunload事件中任何东西,因为iOS的忽略了这一点。任何想法?

And a bunch of other things, but nothing works. We can't even do anything in the onunload event because iOS ignores that too. Any ideas?

推荐答案

我们如何终于解决了这是在版式视图中这样做:

How we finally solved this is doing this in the Layout view:

<script type="text/javascript">
     @Html.Raw("var freshPage = true;")
 </script>

当它贯穿我们的C#code,它告诉我们的页面是新鲜的,并通过我们的控制器code去哪家设置一个javascript变量设置为true。然后我们把这个在我们的全球javascript文件检查变量每当页面被拉升:

Which sets a javascript variable to true when it runs through our C# code that tells us the page is fresh and went through our controller code. Then we put this in our global javascript file to check that variable whenever the page is pulled up:

window.addEventListener('popstate', function () {
  // If fresh page is false that means it is a cached page, remove html and reload page.
  if (!freshPage) {
    $('html').remove();
    window.location.reload();
  } else {
    freshPage = false;
  }
});

这是我们可以发现,当一个页面是从缓存加载的iOS触发的唯一事件。这迫使的iOS,总是通过我们的服务器端code,即使用户使用通过检查变量前进和后退按钮运行。如果这是它看到新的一页是真的第一次页面加载,然后将其翻转为false。然后,如果用户点击后退或前进,回来到这个页面。这code将运行,看到freshPage是假的,因为它没有通过我们的C#code运行,杀HTML,和力的重装该页面。

This is the only event we could find that iOS fires when a page is loaded from the cache. This forces iOS to always run through our server side code even when the user uses the back and forward buttons by checking that variable. If it's the first page load it sees that fresh page is true, then flips it to false. Then if the user hits back or forward and comes back to this page this code will run, see that freshPage is false because it didn't run through our C# code, kill the html, and force a reload of the page.

这篇关于你如何禁用前进和后退缓存iOS上的Safari浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆