兼容解决 IE 、火狐、谷歌浏览器中 Iframe框架的页面缓存的方法

在我们的项目中往往少不了广告的成分,有的时候我们为了方便修改一般会选择iframe嵌套进来的方法,开始的时候没怎么留意,一般自己换广告也是强制刷新,直到后来看广告统计的ip发现好多广告都下架了半个月了可是居然还有点击,才注意到了这一块,那就是浏览器为了加载速度会缓存iframe里面的内容,而好多客户是根本不懂强制刷新的,那么我们就需要做一些操作不让缓存,我们一般会选择后面加上随机数的方法来做。

下面是网上普遍说的方法:

原文是这么说的:

不管是IE还是火狐浏览器,对于iframe 都是有缓存的,可能很多开发的朋友都没有注意,

包子在开发安徽商之都百万网友秒杀盛宴活动 

秒杀专题的时候就遇到类似的状况,即使你的iframe地址是php动态页面也都会出现浏览器缓存,

一开始,我以为只要在php的页面使用header禁止缓存就OK了,但是貌似不行。。额。。

我们先来看下,火狐下解决Iframe框架的页面缓存的方法,

从国外的一个博客上看到的一个方法,原文如下:

Hi

I have a page that contains an iframe. The contents of the iframe are created dynamically,

so every time I come to the page containing this iframe I want to force the iframe to refresh. I added these meta tags to the iframe’s head:
 


<META http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT">
<META http-equiv="Last-Modified" content="Sat, 10 Nov 1997 09:08:07 GMT">
<META http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate">
<META http-equiv="Pragma" content="no-cache"> 
 
and I also add a different random number to the iframe src each time:
 
<IFRAME SRC="http://lab.wehefei.com/index.php?r=xO9X7hEp2wgW5ZTSB38dCrKQnkyq4MA_" WIDTH=900 HEIGHT=600>
 
 
 
It works in Firefox, but in IE6 it still caches the old page.
 
http://lab.wehefei.com
 

额。意思你明白不???嘿嘿,还好,我的计算机英语非常过关,哈哈。,。,。。我不就翻译了。,

很简单的,火狐 中 Iframe框架的页面缓存的方法方法两个:

1、使用上面的meta头信息,当然如果使用php动态页面,你最好也发送header头信息,禁止下缓存

2、添加一个随机数。在html的后面。呵呵

下面。我们来看下,IE中解决iFrame缓存问题的方法有两种:

(1) 每次主页面刷新时随机更换iframe的name;

(2) 每次主页面刷新时在iframe的src路径页面赋予一个随机get参数,例如:

<iframe src=”http://www.example.com/thepage.html” name=”aframe”></iframe>

<script type=”text/javascript”>

document.frames["aframe"].location.href += (document.frames["aframe"].location.href.indexOf(“?”) != -1 ? “?” : “&”) + (new Date()).getTime();

</script>

但是呢,我觉得他这种方法过于麻烦,需要区分IE和谷歌然后分别来写相应的实现方式。

下面来说一种不需要区分就能没有缓存的方法:

<script type="text/javascript">
        document.write('<iframe src="/ad_footer.html?'+ (new Date()).getTime() +'" width="918" scrolling="no" frameborder="0" height="41"></iframe>');
</script>

这种方法经测试有效,也不需要区分浏览器,ok,希望可以帮到大家,如果有其他更简单的方法也可以发布上来。

posted @ 2012-12-28 16:00  雪洁  阅读(14046)  评论(4编辑  收藏  举报