在Cordova InappBrowser中添加按钮以将其隐藏编辑:并通过JavaScript添加图像到InappBro



我在我的离子-V1应用程序中具有带有映射的URL的外部链接。目前,我激活了InappBrowser中的关闭键,我可以从应用程序中再次打开URL。但是,地图上的位置当然是不记得的(它只是重新打开URL(。

所以我在文档中找到了inappbrowser.hide((,这确实对我有帮助。但是,我很难找到在应用程序中添加此方法的方法。最好的方法是什么?

更改当前关闭按钮以进行隐藏而不是关闭(因此分别对Android EN iOS进行操作将JavaScript添加到负载上的InappBrowser中或者…?有人有建议/最佳实践/代码示例吗?谢谢!

编辑:我使用了@nickythewrench的解决方案,但想在正确的徽标(不可单击(的条件下将按钮设置为条。所以我在代码中使用了:

var menu = document.createElement('div'); 
menu.style = 'height:24px;width:100%;background-color:#fdce0c;font-
size:16px;text-align:left;top:0;left:0;position:fixed;'; 
document.body.appendChild(menu); 
var button = document.createElement('Button'); 
button.innerHTML = '≡';
button.style = 'height:24px;border:0px;font-size:16px;border-radius:0px;background-color:#fdce0c;';  
menu.appendChild(button);
var image = document.createElement('Img'); 
image.src = 'http://gerhard.technoleno.nl/VGD_transparent_20px.png';
image.style = 'right:0;position:fixed'
menu.appendChild(image);

这在小提琴中起作用:https://jsfiddle.net/m06hv1yt/16/,但是Ionic Cordova无法提供图像(它使其成为一个蓝色的框,当我在本地保存图像时,有吗?如何将图像添加到这部分JavaScript?

编辑2:编辑答案:URL必须是https,否则离子Cordova找不到。

是的,使用addEventListenerexecuteScript

查看此代码示例,我们在其中注入JavaScript,该javaScript将在InappBrowser中页面顶部生成"隐藏地图"按钮。单击此按钮时,它将在LocalStorage中设置一个新项目,其值为"是"。然后,我们有一个循环检查值是否为"是",并将隐藏InappBrowser。

var ref = window.open('https://www.examplemap.com/', '_blank', 'transitionstyle=fliphorizontal,location=no,toolbarposition=top,closebuttoncaption=X');
// Once the InAppBrowser finishes loading
ref.addEventListener("loadstop", function() {
  // 1st Clear out 'hidden' in localStorage for subsequent opens.
  // 2nd Create the button
  ref.executeScript({
    code: "var key = 'hidden'; var keyval = 'yes'; localStorage.setItem('hidden',''); var button = document.createElement('Button'); button.innerHTML = 'Hide Map'; button.style = 'top:0;right:0;position:fixed;'; document.body.appendChild(button); button.setAttribute('onclick','localStorage.setItem(key,keyval);');"
  });
  // Start an interval
  var loop = setInterval(function() {
    // Execute JavaScript to check if 'hidden' is 'yes' in the
    // child browser's localStorage.
    ref.executeScript({
        code: "localStorage.getItem( 'hidden' )"
      },
      function(values) {
        var hidden = values[0];
        // If 'hidden' is equal to 'yes', clear the interval and hide the InAppBrowser.
        if (hidden === 'yes') {
          clearInterval(loop);
          ref.hide();
        }
      }
    );
  });
});

另外,请注意,在某些情况下,隐藏函数在iOS上不起作用,并且会说"试图在已经隐藏时隐藏IAB" 。如果发生这种情况,请在此处查看解决方案。

我希望这会有所帮助: - (

// On your Cordova js
StatusBar.hide();
var ref=window.open('http://www.foo.bar','_blank','zoom=no,location=no,toolbar=no');
ref.addEventListener("loadstop", function() {
    ref.executeScript({
        code: "localStorage.setItem('close','no');"
    });
    var loop = setInterval(function() {
        ref.executeScript({
            code: "try {localStorage.getItem('close');} catch (exception) {}"
        }, function(values) {
            if (values[0]=== 'yes') {
                clearInterval(loop);
                ref.hide();
            }
        });
    });
});
// On your external page
$("#exitbutton").on("click",function(e){
    window.localStorage.setItem('close','yes');
});

最新更新