简体   繁体   English

JS - 如何从iframe中的链接返回时在页面上重新加载iframe

[英]JS - How to reload iframe on a page when returning back from a link in the iframe

I have a page with an iframe. 我有一个包含iframe的页面。 The following code reloads the page, when it is again on focus. 以下代码重新加载页面,当它再次处于焦点时。 This works fine. 这很好用。

But when I'm navigating away from the page from within the iframe (contains a table with rows), the page should also reload the iframe, when it is in focus again. 但是当我从iframe中导航离开页面时(包含一个包含行的表),当页面再次处于焦点时,页面也应该重新加载iframe。

How can I implement that in vanilla JS (IE8). 我怎样才能在vanilla JS(IE8)中实现它。

  var blurred = false;

  window.onblur = function() { blurred = true; };
  window.onfocus = function() { blurred && (reloadFrames()); };

  function reloadFrames() {
    document.getElementById('iframe1').contentWindow.location.reload();
  } // reloadFrames

Thx for the comments and hints! 感谢评论和提示! This is how I got this working: 这就是我的工作方式:

Implement on main page: 在主页上实现:

  /**
   * Calls reloadFrames when page is on focus again.
   */
  var visibilityChange = (function(window) {
    return function(fn) {
      window.onfocus = function(e) {
        reloadFrames();
      };
    } ();
  } (window)); // Window object


  var reloadFrames = function () {
    document.getElementById('iframe1').contentWindow.location.reload();
  } // reloadFrames

Implement on page that sits in iframe: 在iframe中的页面上实现:

  /**
   * Calls reloadFrames when parent page is on focus again.
   */
  var visibilityChange = (function(parent) {
    return function(fn) {
      window.onfocus = function(e) {
        parent.reloadFrames();
      };
    } ();
  } (parent)); // Parent object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM