call from iframe javascript to parent javascript doesnt work for me

10 views
Skip to first unread message

tma...@gmail.com

unread,
Oct 25, 2013, 2:49:01 PM10/25/13
to node-...@googlegroups.com
Call from  iframe javascript to parent javascript doesnt work for me.

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

-Magizh

tma...@gmail.com

unread,
Nov 6, 2013, 1:52:12 PM11/6/13
to node-...@googlegroups.com, tma...@gmail.com
I tried listening on click event on an element on the iframe using javascript in the parent. Still no positive result :( 

-Magizh 

Ewald Horn

unread,
Nov 8, 2013, 6:59:21 AM11/8/13
to node-...@googlegroups.com, tma...@gmail.com
Hi, I've answered a very similar question on StackOverflow (http://stackoverflow.com/questions/19627591/node-webkit-call-parent-javascript-function-from-iframe).  Basically, you need to pass context into the iframe, in this example I want any links that are clicked in the iframe to open in the default system browser:

<!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.wysetalk.com">WyseTalk</a>
   </footer>
 </body>
</html>

Ewald Horn

unread,
Nov 8, 2013, 7:00:30 AM11/8/13
to node-...@googlegroups.com, tma...@gmail.com
And now I see you are the same guy :)  Sorry for the double post!   On an aside, we actually needed this at work for a project and am using the same code with good results.

tma...@gmail.com

unread,
Nov 8, 2013, 2:13:15 PM11/8/13
to node-...@googlegroups.com, tma...@gmail.com
Thanks work just as expected :) 

-Magizh 

Ewald Horn

unread,
Nov 8, 2013, 2:30:39 PM11/8/13
to node-...@googlegroups.com
Hi Magizh,


That's great! 

All the best,
Ewald


--
You received this message because you are subscribed to the Google Groups "node-webkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-webkit...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages