javascript 在 safari 中弹出警报(iPhone)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4077586/
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
alert pop up in safari(iPhone)
提问by Arun Abraham
How can i change the title (usually the domain) that comes in a window.alert("message") popup in (Safari)iPhone ??
如何更改 (Safari)iPhone 中 window.alert("message") 弹出窗口中的标题(通常是域)?
回答by Evan Mulawski
You would need to use the open-source framework PhoneGap (http://www.phonegap.com/).
您需要使用开源框架 PhoneGap ( http://www.phonegap.com/)。
Then, use:
然后,使用:
navigator.notification.alert("message", callback, "title", "button title");
via Javascript.
通过Javascript。
Edit: This would only be for developing a web app, not for a website. Changing the alert title is not possible.
编辑:这仅适用于开发网络应用程序,不适用于网站。无法更改警报标题。
回答by Zorayr
You may use a generic version that works for both desktop/browser testing environment and PhoneGap/Native environment. Here is what worked for me:
您可以使用适用于桌面/浏览器测试环境和 PhoneGap/Native 环境的通用版本。这是对我有用的:
function showMessage(message, title, callback, buttonName){
title = title || "";
buttonName = buttonName || 'OK';
if(navigator.notification){
navigator.notification.alert(
message, // message
callback, // callback
title, // title
buttonName // buttonName
);
}else{
alert(message);
if(callback)
callback();
}
}
回答by Matt
For anyone wanting to do this without PhoneGap framework, you can pass the data to iOS, then show an alert.
对于想要在没有 PhoneGap 框架的情况下执行此操作的任何人,您可以将数据传递到 iOS,然后显示警报。
In your webview delegate:
在您的 webview 委托中:
- (BOOL) webView:(UIWebView*)webView
shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)type {
NSURL* url = [request URL];
NSString* scheme;
NSString* host;
NSString* path;
BOOL isRealUrl = YES;
switch (type) {
case UIWebViewNavigationTypeLinkClicked:
// Open link in Safari
[[UIApplication sharedApplication] openURL:url];
return NO;
break;
case UIWebViewNavigationTypeFormSubmitted:
case UIWebViewNavigationTypeOther:
scheme = [url scheme];
host = [url host];
path = [url path];
if ([scheme isEqualToString:@"alert"]) {
[[[UIAlertView alloc] initWithTitle:host
message:[path substringFromIndex:1]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
isRealUrl = NO;
} else {
// Go to another page in your app.
isRealUrl = YES;
}
break;
default:
break;
}
return isRealUrl;
}
In your javascript:
在你的 javascript 中:
function myAlert(message, title) {
if (/iphone|ipod|ipad/.test(navigator.userAgent) &&
!/safari/i.test(navigator.userAgent)) {
document.location.href = 'alert://' + encodeURIComponent(title) + '/' +
encodeURIComponent(message);
} else {
alert(message);
}
}
Then call the alert function myAlert('Testing', 'One, Two, Three');
然后调用alert函数 myAlert('Testing', 'One, Two, Three');
Notice, the scheme alertmust match in the delegate function and the javascript href.
请注意,该方案alert必须在委托函数和 javascript href 中匹配。

