在父窗口调用iframe里面的函数(各浏览器兼容)

来源:互联网 发布:怎么样盗用别人软件 编辑:程序博客网 时间:2024/05/04 15:42
I struggled with this problem for a while and finally found a rocksolid solution through a posting by Bobince in Stackoverflow (here):
Instead of searching the function from the parent side, it is much saver and more predictable to hand over a function object from the IFrame side. This also makes it very easy to check if the IFrame function has already loaded. It works like a charm in Chrome, FF, IE 9, 8 and even 7.

Here is an example:

TESTouter.html
Copy code
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var gIframeFunc = undefined;
  5. function callIframeFunc(){
  6. if(typeof gIframeFunc != 'undefined'){
  7. gIframeFunc();
  8. }else{
  9. alert('Iframe function not available')
  10. }
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. <iframe id="myIframe" src="TESTinner.html"></iframe>
  16. <button onclick="callIframeFunc();">Access function</button>
  17. </body>
  18. </html>
TESTinner.html
Copy code
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function iframeFunc(){
  5. alert('Iframe function IS WORKING')
  6. }

  7. parent.gIframeFunc = iframeFunc;
  8. </script>
  9. </head>
  10. <body>
  11. This is the iframe body.
  12. </body>
  13. </html>

Cheers, Jörg.

 

 

来自Calling Function inside an iFrame

原创粉丝点击