javascript 刷新 iframe 而不刷新整个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21937168/
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-27 22:02:26  来源:igfitidea点击:

Refresh an iframe without refreshing the entire page

javascriptjqueryiframerefreshreload

提问by Travis2375

I have an iframe that I would like to automatically refresh itself without doing a whole page refresh. I would prefer to use javascript or jquery. This is what I have been trying unsuccessfully.

我有一个 iframe,我想在不刷新整个页面的情况下自动刷新它。我更喜欢使用 javascript 或 jquery。这是我一直尝试失败的方法。

<div class="Widget"><!--DARK SKY-->
    <div class="WidgetHeader">
        <img src = "images/WidgetHeaders/DarkSky.png"></div>
    <div id="DarkSky" style="width: auto; height: 245px; padding: 5px; background-color: hsla(0,0%,100%,.9);">
        <iframe id="forecast_embed" type="text/html" frameborder="0" height="245" width="100%" src="http://forecast.io/embed/#lat=38.269571&lon=-85.487420&name=Louisville&units=us&color=#4c7cba"> </iframe>
    </div>   
</div>

<script type = "text/javascript">
    window.onload = function() {   
        setInterval(function refreshDarkSky() {  
            document.getElementById("#forecast_embed").src ="http://forecast.io/embed/#lat=38.269571&lon=-85.487420&name=Louisville&units=us&color=#4c7cba"
        }, 90000);
    } 
</script>

回答by Milind Anantwar

if iframe is not from different domain, you can use:

如果 iframe 不是来自不同域,则可以使用:

document.getElementById('forecast_embed').contentDocument.location.reload(true);

for cross domain,

对于跨域,

var iframe = document.getElementById('forecast_embed');
iframe.src = iframe.src;

回答by Anoop Joshi

try this code

试试这个代码

$(document).ready(function () {
            setInterval(function refreshDarkSky() {
                $("#forecast_embed").attr("src","http://forecast.io/embed/#lat=38.269571&lon=-85.487420&name=Louisville&units=us&color=#4c7cba"); 
            }, 90000);
        });

回答by jiten jethva

Use this:

用这个:

$('#forecast_embed',window.parent.document).attr('src',
             $('#forecast_embed',window.parent.document).attr('src'));

It has to untie your issue.

它必须解开你的问题。

回答by Mohit maru

try this

试试这个

window.top.location.reload();