---
title: "解决iframe缓存"
author: "weistar"
date: "2013-12-18T17:13:21Z"
tags: []
categories: ["Web前端"]
slug: "iframe"
draft: true
lastmod: "2013-12-18T17:15:54Z"
id: 1991657
url: "https://weistar.iteye.com/blog/1991657"
---

发现iframe有缓存的问题。网上提供了思路，即对iframe的href后添加随即get值，来逃避iframe的缓存。

 

所以干脆直接对指向iframe的&lt;a&gt;添加 js：

 

`onClick="javascript:this.href=this.href+'?new='+Math.random();alert(this.href)"`

最后在index页面为了第一次加载的iframe添加：

```
window.onload=function(){
　　window.frames[0].location.href=window.frames[0].location.href+'?new='+Math.random();
｝
```

 注意要使用window.onload，因为iframe可以和主页面并行加载，不会阻塞主页面，但是会阻塞onload事件（因此有时也可通过iframe异步加载大的js组件）。