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