我用几种方式在谷歌上搜索了这个问题,但只能找到在使用PhoneGap或jQuery mobile的情况下的答案。然而,我既不使用PhoneGap也不使用jQuery mobile,只是使用普通的html和javascript。
我正在尝试使用window.open()从全屏web应用程序启动移动版Safari浏览器... 而不是内联锚点。无论我做什么,url都会在web应用程序中打开,而不是在Safari中打开。有人有什么建议吗?
谢谢。
我用几种方式在谷歌上搜索了这个问题,但只能找到在使用PhoneGap或jQuery mobile的情况下的答案。然而,我既不使用PhoneGap也不使用jQuery mobile,只是使用普通的html和javascript。
我正在尝试使用window.open()从全屏web应用程序启动移动版Safari浏览器... 而不是内联锚点。无论我做什么,url都会在web应用程序中打开,而不是在Safari中打开。有人有什么建议吗?
谢谢。
我花了一段时间才成功地拼凑出这个解决方案。在iOS独立的Web应用模式中,它使用jQuery创建一个链接元素,将其添加到页面主体,模拟单击操作,然后删除该链接。即使没有jQuery,也可以通过使用原生DOM方法来实现相同的功能,只是需要更多的代码。
if (window.navigator.standalone) {
var $a = $('<a href="' + url + '" target="_blank"/>');
$("body").append($a);
var a = $a.get(0);
var mouseEvent = a.ownerDocument.createEvent('MouseEvents');
mouseEvent.initMouseEvent('click');
a.dispatchEvent(mouseEvent);
$a.remove();
}
else {
window.open(url, '_blank');
}
从iOS 4.3开始,我所知道的唯一方法是将<div>
转换为<a target="_blank">
,并让默认浏览器来处理它。它会在外部Safari中打开新页面。
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
AppDelegate.m
中寻找但没有成功的人,请注意,在较新版本的PhoneGap/Apache Cordova中(包括1.8.0),webView:shouldStartLoadWithRequest:navigationType:
方法的生成覆盖已被注释并移动到MainViewController.m
的底部。 - Daniel Trebbienvar a = document.createElement("a");
a.setAttribute('href', facebook);
a.setAttribute('target', '_blank');
a.click();
你不能使用一个普通的A HREF标签吗?这通常会在Web应用程序内部打开Safari。
你尝试过使用window.openNative()
吗?