【问题标题】:How to refresh a page (after x seconds) opened in an iframe?如何刷新在 iframe 中打开的页面(x 秒后)?
【发布时间】:2019-05-09 15:16:45
【问题描述】:

我在 iFrame 中打开了一个页面。我想在 x 秒后刷新页面(x 可以是我得到的任何大于 5 的数字)。我怎么能做到这一点?

这是我的代码;

<iframe sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts" class="iframe" ng-show="$ctrl.getFrameURL() !== 'Default URL'" ng-src="{{$ctrl.getFrameURL()}}" width="100%" height="100%" scrolling="yes" frameBorder="0" id="remotePageFrame"></iframe>

我检查了 StackOverflow 上的一些问题,人们通过更新范围重新加载了页面。我不想更新范围,因为范围变量中的 URL 值保持不变。 JavaScript 或 AngularJs 中是否有任何东西可以帮助刷新 iframe 中的页面。

Update-1

@sarvon ks 建议的解决方案适用于我自己的页面,这些页面托管在我的项目中,但是当我尝试刷新任何外部/第三方页面时它会抱怨。在我的页面中,我有一个 iFrame 循环,它打开了多个页面。其中大部分是外部页面。对于外部页面,我得到了 Uncaught SecurityError 异常。

-- Uncaught SecurityError: Blocked a frame with origin "http://localhost:63342" from accessing a cross-origin frame.

existing question 已经很好地讨论了这个错误。是否有任何其他解决方法可以刷新在 iFrame 中打开的页面?

【问题讨论】:

    标签: javascript angularjs iframe


    【解决方案1】:

    检查此代码它将起作用

    
        var duration = 100;
        function reloadIframe() {
              var iframeDome = document.getElementsByTagName("iframe");
                iframeDome[0].contentWindow.location.reload();
        }
    
        setInterval(reloadIframe,duration);
    
    
        <iframe src="https://www.google.com"></iframe>
    

    【讨论】:

    • 谢谢@sarvon ks。我已经更新了我的问题。您提供的解决方案适用于同源页面。有没有办法重新加载第三方页面?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2012-06-18
    • 2013-03-20
    • 1970-01-01
    • 2016-02-20
    • 2014-02-10
    • 1970-01-01
    相关资源
    最近更新 更多