2010-11-02 97 views
4

如何更改在(Safari)iPhone中的window.alert(“消息”)弹出窗口中出现的标题(通常是域)?在safari(iPhone)中弹出警报

+1

那么你不能为任何普通的浏览器做到这一点,所以我打赌你不能为iPhone浏览器做到这一点。 – epascarello 2010-11-02 12:33:41

回答

5

您需要使用开源框架PhoneGap(http://www.phonegap.com/)。

然后,使用:

navigator.notification.alert("message", callback, "title", "button title"); 

通过JavaScript。

编辑:这只会用于开发一个Web应用程序,而不是一个网站。更改警报标题是不可能的。

2

您可以使用适用于桌面/浏览器测试环境和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(); 
    } 

} 
0

任何人想要做到这一点没有的PhoneGap框架,您可以将数据传递到iOS,然后显示一个警告。

在你的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; 
} 

在你的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); 
    } 
} 

然后调用报警功能myAlert('Testing', 'One, Two, Three');

通知,该方案alert必须委托函数和匹配javascript href。