2010-11-02 94 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。