javascript 在 iframe 中刷新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11043323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:54:47  来源:igfitidea点击:

Refresh page within an iframe

javascripthtmlfacebookinternet-explorerrefresh

提问by DarthVader

I have an app (facebook canvas app, facebook displays the app in an iframe)

我有一个应用程序(facebook 画布应用程序,facebook 在 iframe 中显示该应用程序)

When the user clicks on a link I want to be able to refresh the page.

当用户单击链接时,我希望能够刷新页面。

the code i m using to refresh the page is as follows:

我用来刷新页面的代码如下:

window.location.reload(true);

With firefox this works fine, it only refreshes the current page within the iframe. So i m happy.

使用 Firefox 可以正常工作,它只会刷新 iframe 中的当前页面。所以我很高兴。

With IE, the whole page is being refreshed which causes the user to go the beginning of the app.

使用 IE,整个页面都会被刷新,这会导致用户进入应用程序的开头。

How can i avoid that?

我怎样才能避免这种情况?

I just want refresh the current page not the current url.

我只想刷新当前页面而不是当前 url。

apps.facebook.com/foo/

is the app URL and within URL user clicks on a link go to a page, then within that Page i need to refresh that page to update some counters. but when i refresh the whole request goes to apps.facebook.com/foo/

是应用程序 URL,在 URL 中,用户点击链接转到页面,然后在该页面内,我需要刷新该页面以更新一些计数器。但是当我刷新整个请求时apps.facebook.com/foo/

any ideas?

有任何想法吗?

回答by Dan

var oiframe = document.getElementById('myiframe');

oiframe.contentWindow.location.reload(true);

回答by Ajay Beniwal

you can use window.location.href = window.location.href

您可以使用 window.location.href = window.location.href

回答by xori

As stated in http://www.hyperorg.com/blogger/2007/03/24/refresh-an-iframe-in-ie-anyone/

http://www.hyperorg.com/blogger/2007/03/24/refresh-an-iframe-in-ie-anyone/ 所述

HTML

HTML

<div id="wrapper"></div>

Javascript

Javascript

function reload () {
    var fr=document.getElementById('tehframe');
    if(fr!=null) document.getElementById("wrapper").removeChild(fr);
    var iframehtml="<iframe id='tehframe' src=…></iframe>";
    document.getElementById("wrapper").innerHTML=iframehtml;
}

Then just call reload()at the beginning and whenever you want to load it.

然后只需reload()在开始时调用,只要你想加载它。

回答by Tom

You can change the url of the iframe and cause a refresh by adding a meaningless parameter to the url eg foo becomes foo?bar

您可以更改 iframe 的 url 并通过向 url 添加一个无意义的参数来引起刷新,例如 foo 变为 foo?bar

回答by Bramovic44

You can use this trick :

你可以使用这个技巧:

  <button onclick="window.parent.location = document.referrer">
    Reload
  </button>