微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node-webkit – 从iframe调用父javascript函数

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

我试图在认浏览器上的iframe中启动链接
我想在父窗口中调用一个函数,以便调用

gui.Shell.openExternal("link");

任何帮助表示赞赏.提前致谢.

解决方法

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

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

尝试这个:

<!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>

原文地址:https://www.jb51.cc/html/231601.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐