Preventing iframe caching in browser

前端 未结 13 1412
自闭症患者
自闭症患者 2020-11-29 18:03

How do you prevent Firefox and Safari from caching iframe content?

I have a simple webpage with an iframe to a page on a different site. Both the outer page and the

相关标签:
13条回答
  • 2020-11-29 18:30

    Have you tried adding the various HTTP Header options for no-cache to the iframe page?

    0 讨论(0)
  • 2020-11-29 18:34

    I found this problem in the latest Chrome as well as the latest Safari on the Mac OS X as of Mar 17, 2016. None of the fixes above worked for me, including assigning src to empty and then back to some site, or adding in some randomly-named "name" parameter, or adding in a random number on the end of the URL after the hash, or assigning the content window href to the src after assigning the src.

    In my case, it was because I was using Javascript to update the IFRAME, and only switching the hash in the URL.

    The workaround in my case was that I created an interim URL that had a 0 second meta redirect to that other page. It happens so fast that I hardly notice the screen flash. Plus, I made the background color of the interim page the same as the other page, and so you notice it even less.

    0 讨论(0)
  • 2020-11-29 18:34

    I also had this problem in 2016 with iOS Safari. What seemed to work for me was giving a GET-parameter to the iframe src and a value for it like this

    <iframe width="60%" src="../other/url?cachebust=1" allowfullscreen></iframe>

    0 讨论(0)
  • 2020-11-29 18:34

    If you want to get really crazy you could implement the page name as a dynamic url that always resolves to the same page, rather than the querystring option?

    Assuming you're in an office, check whether there's any caching going on at a network level. Believe me, it's a possibility. Your IT folks will be able to tell you if there's any network infrastructure around HTTP caching, although since this only happens for the iframe it's unlikely.

    0 讨论(0)
  • 2020-11-29 18:39

    I have been able to work around this bug by setting a unique name attribute on the iframe - for whatever reason, this seems to bust the cache. You can use whatever dynamic data you have as the name attribute - or simply the current ms or ns time in whatever templating language you're using. This is a nicer solution than those above because it does not directly require JS.

    In my particular case, the iframe is being built via JS (but you could do the same via PHP, Ruby, whatever), so I simply use Date.now():

    return '<iframe src="' + src + '" name="' + Date.now() + '" />';
    

    This fixes the bug in my testing; probably because the window.name in the inner window changes.

    0 讨论(0)
  • 2020-11-29 18:40

    Make the URL of the iframe point to a page on your site which acts as a proxy to retrieve and return the actual contents of the iframe. Now you are no longer bound by the same-origin policy (EDIT: does not prevent the iframe caching issue).

    0 讨论(0)
提交回复
热议问题