2013-10-28 97 views
1

在node-webkit中,從iframe調用javascript到父javascript不適用於我。
從iframe調用父javascript函數

我嘗試推出的默認瀏覽器在iframe中的鏈接,結果 我想調用的函數的父窗口,以撥打:

gui.Shell.openExternal("link"); 

任何幫助表示讚賞。提前致謝。

+0

什麼是你試圖打開URL? – zaerymoghaddam

+0

我想打開一個鏈接到其他服務器運行在別處。如果我可以讓google.com在默認瀏覽器中打開就足夠了。 – magizh

回答

3

你想要做的是截取內部框架中的鏈接。

這裏我們有一個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> 
+0

感謝那些作品:) – magizh

+0

快樂 - 節點Webkit是如此強大的工具! – Ewald

+0

如果您在iframe中加載外部網站,這不起作用,對吧?您會收到錯誤:'SecurityError:阻止了一個源於「http:// ...」的框架訪問源於「http:// ...」的框架。協議,域和端口必須匹配' – aaaaahaaaaa