views:

641

answers:

5

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 inner page have HTTP response headers to prevent caching. When I click the "back" button in the browser, the outer page works properly, but no matter what, the browser always retrieves a cache of the iframed page. IE works just fine, but Firefox and Safari are giving me trouble.

My webpage looks something like this:

<html>
  <head><!-- stuff --></head>
<body>
  <!-- stuff -->
  <iframe src="webpage2.html?var=xxx" />
  <!-- stuff -->
</body>
</html>

The var variable always changes. Despite the fact that the URL of the iframe has changed (and thus, the browser should be making a new request to that page), the browser just fetches the cached content.

I've examined the HTTP requests and responses going back and forth, and I noticed that even if the outer page contains <iframe src="webpage2.html?var=222" />, the browser will still fetch webpage2.html?var=111.

Here's what I've tried so far:

  • Changing iframe URL with random var value
  • Adding Expires, Cache-Control, and Pragma headers to outer webpage
  • Adding Expires, Cache-Control, and Pragma headers to inner webpage

I'm unable to do any JavaScript tricks because I'm blocked by the same-origin policy.

I'm running out of ideas. Does anyone know how to stop the browser from caching the iframed content?

Update

I installed Fiddler2 as Daniel suggested to perform another test, and unfortunately, I am still getting the same results.

This is the test I performed:

  1. Outer page generates random number using Math.random() in JSP.
  2. Outer page displays random number on webpage.
  3. Outer page calls iframe, passing in random number.
  4. Inner page displays random number.

With this test, I'm able to see exactly which pages are updating, and which pages are cached.

Visual Test

For a quick test, I load the page, navigate to another page, and then press "back." Here are the results:

Original Page:

  • Outer Page: 0.21300034290246206
  • Inner Page: 0.21300034290246206

Leaving page, then hitting back:

  • Outer page: 0.4470929019483644
  • Inner page: 0.21300034290246206

This shows that the inner page is being cached, even though the outer page is calling it with a different GET parameter in the URL. For some reason, the browser is ignoring the fact that the iframe is requesting a new URL; it simply loads the old one.

Fiddler Test

Sure enough, Fiddler confirms the same thing.

(I load the page.)

Outer page is called. HTML:

0.21300034290246206
<iframe src="http://ipv4.fiddler:1416/page1.aspx?var=0.21300034290246206" />

http://ipv4.fiddler:1416/page1.aspx?var=0.21300034290246206 is called.

(I navigate away from the page and then hit back.)

Outer page is called. HTML:

0.4470929019483644
<iframe src="http://ipv4.fiddler:1416/page1.aspx?var=0.4470929019483644" />

http://ipv4.fiddler:1416/page1.aspx?var=0.21300034290246206 is called.

Well, from this test, it looks as though the web browser isn't caching the page, but it's caching the URL of the iframe and then making a new request on that cached URL. However, I'm still stumped as to how to solve this issue.

Does anyone have any ideas on how to stop the web browser from caching iframe URLs?

A: 

Have you installed Fiddler2?

It will let you see exactly what is being requested, what is being sent back, etc. It doesn't sound plausible that the browser would really hit its cache for different URLs.

Daniel Earwicker
Thanks for the tip on Fiddler2. Now I know that the browser isn't caching the iframe content; it's simply caching the iframe URL. However, I'm still stumped, and I have no idea why the browser would ignore the iframe URL of the new page and simply just use the old URL.
Zarjay
+1  A: 

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.

kmoser
A: 

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

Chris Webb
Yup. I've tried no-cache directives in both the HTTP headers and the meta tags on each page.
Zarjay
A: 

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.

Chris Webb
+1  A: 

This is a bug in Firefox:

https://bugzilla.mozilla.org/show_bug.cgi?id=356558

Try this workaround:

<iframe src="webpage2.html?var=xxx" id="theframe"></iframe>

<script>
var _theframe = document.getElementById("theframe");
_theframe.contentWindow.location.href = _theframe.src;
</script>
Caleb