How to access cordova functions from an IFrame in

2019-05-19 13:48发布

问题:

IN my Phonegap ios project i have an i frame and src of Iframe is a local html page within the same www folder . inside the html (IFrame source) one button and onlick listener. i need to open one website while clicking that button in InAppBrowser or in safari (new window). i cannot able to access phonegap methods from the html file (IFrame source), i includeed cordova on both html files, my source html page given bellow, this page is shown in an Iframe.

<html>

<head>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="js/libs/jquery.js"></script>
<script src="adv.js"></script>

<script>


  $(document).ready(function () {

      $('#advArea').find('button').on('click', function (e) {
                                 e.preventDefault();
                                 var path = "http://www.google.com");

                                 console.log(path);
                                 //window.open(path,'__system');
                                 //need to acces inApp Browser from here

                                 })

  });

</script>
</head>

<body>
<div id="advArea">

 <button></button>
  </div>
</body>

</html>

回答1:

In your "parent" page where your iFrame is, create the below function

function popnew(path) {
        navigator.app.loadUrl(link, { 'openExternal' : true });
    }

then you call the above function for any button/link in the iFrame

<button onclick="javascript:parent.popnew('http://www.stackoverflow.com')"></button>