刷新位于不同域中的iframe

Refreshing an iframe which are in different domains

本文关键字:iframe 于不同 刷新      更新时间:2024-05-05

我有一个名为main.jsp的页面,它位于域domain1中,并且它有一个从domain2加载内容的iframe。基本上,main.jsp是一种常见的内容,在iframe中,我们从部署在不同服务器上的其他web应用程序加载内容。

我的问题是我想自动刷新iframe中的内容(比如5秒)。我首先尝试了这个代码:

<meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />" />

Err: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame

我试过这个代码:

<script type="text/javascript">
   window.setTimeout(function(){ window.location.reload() }, 15000);
</script>

这也给了我同样的错误。有人能指导我如何做到这一点吗?

注:我添加了这个代码来消除跨域问题:

<script type="text/javascript">
 document.domain = window.location.hostname.replace('www.', '');
</script>

尝试动态添加

   function addRefresh(){
       var meta = document.createElement('meta');
       meta.setAttribute("http-equiv", "refresh");
       meta.setAttribute("content", "5");
       document.getElementsByTagName('head')[0].appendChild(meta);
    }
    if(location.origin === 'domain1.com'){
      addRefresh();
    }