开发者

How to call an iframe to do something?

Here is the situation.... I have a iframe, which is located to my client side, and the iframe content is in my server. So, when the user do something, my iframe will appear, and they can type something, and post to my server. But the question is....The iframe itself can't disappear. The only thing I want to do, is submit and hide the i开发者_JAVA技巧frame. I can do a button in my client side to hide my iframe, but the iframe can't control the js in the client side. And the client side also can't control button on my iframe. So, I am thinking to make a button in the client side, and some kind of command to submit the button....But the question is how can I do so? Thank you.


The iframe and the parent document can communicate through javascript:

  • the iframe is available to the document like this: window.frames["iframe_name"]. This is the window object of the iframe, from here on you can do just about anything with js.
  • the parent document's window is available to the iframe through window.opener. Again, this is the window of the parent document.

Maybe this question helps you too.


for even more robustness:

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

and

...
var el = document.getElementById('iframe_id');
// or
var el = window.frames["iframe_name"];
// or
var el = window.frames[0];

var frame_win = getIframeWindow(el);

if (frame_win) {
  frame_win.targetFunction();
  ...
}
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜