从iframe调用父javascript函数 [英] call parent javascript function from iframe

查看:74
本文介绍了从iframe调用父javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在node-webkit中,从iframe javascript到父javascript的调用对我不起作用.

In node-webkit, call from iframe javascript to parent javascript doesnt work for me.

因此,我尝试在默认浏览器的iframe中启动链接 我想在父窗口中调用一个函数来调用:

I am trying to launch a link in the iframe on the default browser as a result I want to call a function in the parent window so as to call:

gui.Shell.openExternal("link");

感谢您的帮助.提前致谢.

Any help is appreciated. Thanks in advance.

推荐答案

您想要做的是拦截内部框架中的链接.

What you want to do, is to intercept links in the internal frame.

在这里,我们有一个iframe,所有链接都将在默认浏览器中打开,而不是在Node WebKit上下文中打开.我希望这会有所帮助.

Here we have an iframe where all links will open in the default browser, not in the Node WebKit context. I hope this helps.

尝试一下:

<!DOCTYPE html>
<html>
 <head>

  <script type="text/javascript">
    window.gui = require('nw.gui');

    handleLinks = function(event)
    {
            var href;

            function checkLinks(element) 
            {
                if (element.nodeName.toLowerCase() === 'a') 
                {
                    href = element.getAttribute('href');
                    if (href)
                    {
                        gui.Shell.openExternal(href);
                        // important, prevent the default event from happening!
                        event.preventDefault();
                    }   
                }                   
                else if (element.parentElement) 
                {
                  checkLinks(element.parentElement);
                }
            }
            checkLinks(event.target);
    };

     function isLoaded() 
     {
       // let's see if the iframe has finished loading
       var iframe = document.getElementById('myframe');

       if (iframe && iframe.contentWindow && iframe.contentWindow.document &&
           iframe.contentWindow.document.body &&
           iframe.contentWindow.document.body.innerHTML) 
           {
            //now deal with links
            iframe.contentWindow.document.body.addEventListener('click', handleLinks, false);
           } 
           else 
           {
             // not yet, let's wait a bit and try again
             setTimeout(isLoaded, 300);
           }
     };
   </script>
 </head>
 <body>
   <iframe id="myframe" src="http://www.google.com" onLoad="isLoaded();" style="width: 100%;" seamless="true" nwdisable nwfaketop></iframe>
   <div>
    Links in the normal browser should still work in the Node Webkit environment.
   </div>
   <footer>
    <a href="http://www.yoursitehere.com">Whaddayaknow</a>
   </footer>
 </body>
</html>

这篇关于从iframe调用父javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆