javascript 在 PhoneGap 中调用 window.location.href 触发网络浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5897955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:44:12  来源:igfitidea点击:

Calling window.location.href in PhoneGap triggers web browser

javascriptipadbrowsercordovawindow.location

提问by Claus

Hello I'm trying to develop an application for iPad using PhoneGap. I would like to dinamically load inside the index.html page the main page of an external website. Unfortunately using

您好,我正在尝试使用 PhoneGap 为 iPad 开发应用程序。我想在 index.html 页面内动态加载外部网站的主页。不幸的是使用

window.location.href = "http://mywebsite.com/cgi-bin/index.py"

triggers the opening of a Safari window instead of using the PhoneGap container.

触发 Safari 窗口的打开,而不是使用 PhoneGap 容器。

Any suggestions?

有什么建议?

Thanks a lot

非常感谢

Claus

克劳斯

采纳答案by John D

Find the AppDelegate.m file in the 'Classes' part of the project, and find webView:shouldStartLoadWithRequest:navigationType Make the function look like this and try again!

在项目的'Classes'部分找到AppDelegate.m文件,找到 webView:shouldStartLoadWithRequest:navigationType 把函数改成这个样子,再试一次!

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];    
        return NO;
    }
    else {
       return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

回答by Stin

There's a simpler option: modify config.xml

有一个更简单的选择:修改 config.xml

Open all links in WebView

stay-in-webview with values true or false

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

  • if set to true, all links (even with target set to blank) will open in the app's webview

  • only use this preference if you want pages from your server to take over your entire app

  • default is false

在 WebView 中打开所有链接

值 true 或 false 的留在 webview

  • 例子: <preference name="stay-in-webview" value="true" />

  • 如果设置为 true,所有链接(即使目标设置为空白)都将在应用程序的 webview 中打开

  • 仅当您希望来自服务器的页面接管整个应用程序时才使用此首选项

  • 默认为假

Source: https://build.phonegap.com/docs/config-xml

来源:https: //build.phonegap.com/docs/config-xml