页面更改后如何刷新iframe?使用AJAX? [英] How to refresh a iframe when the page changes? With AJAX?

查看:47
本文介绍了页面更改后如何刷新iframe?使用AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当页面更改时是否可以刷新iframe?(iframe中的页面-不是iframe所在的页面)我想让iframe显示正在开发的页面,然后每当页面更改/更新时,我都希望iframe刷新以便显示较新的版本.希望这是有道理的.:P
还是使用其他东西代替iframe更好?iframe现在过时了吗?

Is it possible to refresh an iframe whenever the page changes? (The page within the iframe - not the page the iframe is on) I want to have an iframe show a page which is being developed, then whenever the page is changed/updated I want the iframe to refresh so it shows the newer version. Hope that makes sense. :P
Or would it be better to use something else instead of an iframe? Are iframes outdated now?

推荐答案

仅因为我发现这很有趣...使用jQuery:

Only because I find this interesting... using jQuery:

<iframe id="someiFrame"></iframe>

<script type="text/javascript">
var page = '/some/page/on/this/server.html', lM;

function checkModified(){
   $.get(page, function(a,a,x){
      var mod = x.getResponseHeader('last-modified');
      if(lM != mod){
         lM = mod;
         $('#someiFrame').attr('src', page);
      }
   }
}

setInterval(checkModified, 5000); // every 5 seconds
</script>

这将每5秒轮询一次页面(这非常浪费,但是如果它在本地开发机上,那又会怎样呢?)并仅在页面更新时重新加载iframe:)

That will poll the page every 5 seconds (incredibly wasteful but if it's on a local dev machine, so what?) and reload the iframe only when the page is updated :)

请注意,iFrame必须与父页面位于同一域中.

Note that the iFrame MUST be on the same domain as the parent page.

这篇关于页面更改后如何刷新iframe?使用AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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