开发者

How to call iframe's function from this iframe

开发者 https://www.devze.com 2022-12-29 03:37 出处:网络
I have an iframe created in Javascript with 开发者_Go百科some function f(): var iframe = document.createElement(\"iframe\");

I have an iframe created in Javascript with 开发者_Go百科some function f():

var iframe = document.createElement("iframe");
$(iframe).attr({
  width: 0,
  height: 0,
  frameborder: 0,
  src: this.options.url,
  name: id,
  id: id
});

document.body.appendChild(iframe);

iframe.contentWindow.f = function(data) {
 alert("test");
};

Document loaded in this iframe should call function f():

<script ...>f();</script>

And this works perfectly in Firefox but Opera tells, f() is undefined.

Is there any solution?

Adrian.


It's a good change there is a race condition so that the document in the iframe has finished loading before you set the function. It probably would be better to have the iframe document call a function in the parent document instead.

0

精彩评论

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