Javascript window object

前端 未结 2 1667
误落风尘
误落风尘 2021-02-13 18:26

In Javascript, let\'s say we have a main page (main.html) which contains an

Parent's JavaScript:

function foo() {
  display("foo called!");
}
function display(msg) {
  var p = document.createElement('p');
  p.innerHTML = msg;
  document.body.appendChild(p);
}

Child's HTML:

I'm in the frame

Child's JavaScript:

window.onload = function() {

  document.getElementById('theButton').onclick = function() {
    var p = window.parent;
    if (!p) {
      display("Can't find parent window");
    }
    else if (typeof p.foo !== "function") {
      display("Found parent window, but can't find foo function on it");
    }
    else {
      display("Calling parent window's foo function.");
      p.foo();
    }
  };

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }

};

提交回复
热议问题