在 webview 中打开 url - phonegap

新手上路,请多包涵

我想知道如何在嵌入 webview 的应用程序上下文中打开 url。目前这个演示将在外部浏览器中打开一个新选项卡,所以,不是我所期望的。我使用 google.com 只是为了测试。

总结,我正在寻找一个功能演示。

 <?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        xmlns:android = "http://schemas.android.com/apk/res/android"
        id        = "com.xxx.xxxxx"
        version   = "1.0.0">

    <preference name="stay-in-webview" value="true" />

    <access origin="*" browserOnly="true" subdomains="true" />

    <content src="index.html" />

    <allow-navigation href="https://google.com/*" />

    <gap:plugin name="cordova-plugin-whitelist" source="npm" version="~1" />
    <gap:plugin name="org.apache.cordova.inappbrowser" />
    <gap:plugin name="org.apache.cordova.splashscreen" />

    <preference name="phonegap-version"           value="cli-5.4.1" />
    <preference name="permissions"                value="none"/>
    <preference name="target-device"              value="universal"/>
    <preference name="fullscreen"                 value="true"/>

</widget>


 <!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/index.css" />
    </head>
    <body>
        <div>
            <script type="text/javascript" charset="utf-8">
                document.addEventListener("deviceready", onDeviceReady, false);

                function onDeviceReady() {
                    window.location.href = 'https://google.com';
                }
            </script>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
    </body>
</html>

更新:完整的 xml 文件: https ://codeshare.io/Vw3Fl

原文由 user2990084 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 569
2 个回答

尝试 :

 window.open('https://google.com', '_self ', 'location=yes');

代替 :

 window.location.href = 'https://google.com';

这将使用 InAppBrowser,并使用 _self 作为目标。

原文由 tnt-rox 发布,翻译遵循 CC BY-SA 3.0 许可协议

您必须在 config.xml 中添加这一行以允许导航到外部 url

 <allow-navigation href="*" />

这将允许导航到任何外部 url,如果您只想允许导航到 google,则添加此行

<allow-navigation href="https://google.com" />

您可以 在插件页面上 查看其余文档

https://github.com/apache/cordova-plugin-whitelist

原文由 jcesarmobile 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏