我实现了这一点,因为我需要相同的解决方案。我假设您已经知道如何加载本地文件,但以防万一......
//By "SomeLocalFile.html" I mean an html file in the /www/ folder of the actual app installation **not** on the server
var ref = window.open('somelocalfile.html', '_blank', 'location=no');
首先将这样的事件侦听器添加到您从中调用 var ref = window.open() 的应用程序的主 js。
ref.addEventListener('loadstart', LoadStart); // in YourMainJs.js
然后在 /www/ 文件夹中的某个位置创建一个名为 closeInAppBrowser.html 的本地文件,碰巧地雷位于 /www/pages/ 中
现在定义 LoadStart 函数,以便在页面开始加载时 LoadStart() 将触发
//I use match(/[^/]+$/g) because I find that matching the file name is just easier than the path.
// This code is place in in YourMainJs.js
fileName = event.url.match(/[^/]+$/g);
if(fileName[0] == "closeInAppBrowser.html"){
// alert("fun load stop runs");
ref.close();
}
现在在您将要使用 window.open() 的 SomeLocalFile.html 中,放置一个链接和像这样的 js。
// in SomeLocalFile.html that was called via the window.open()
<a class="close">Close This Window Like A BOSS!</a>
$('.close').click(function(){
//We use window location cause you can't navigate to any other local file just by using an href in an <a> tag
//We aslo use ../../ because that is the correct path usage from within InAppBrowser to another local file
window.location = '../../pages/closeInAppBrowser.html';
});
现在,当您单击该链接时,它将尝试导航到 closeInAppBrowser.html,这将触发 LoadStart(),它将检查 event.url 文件名是否与“closeInAppBrowser.html”匹配,如果匹配,它将关闭窗口。
我已经对此进行了测试,它可以 100% 工作。如果您有任何其他问题,请告诉我