UIWebView:我可以在任何网页中禁用javascript alert()吗?

时间:2022-03-14 06:45:51

I am using UIWebView to load a URL.

我正在使用UIWebView加载URL。

Inside the page of that URL, it uses alert("whatever msg") as JavaScript. My UIWebView will pop up a window and show that alert message.

在该URL的页面内,它使用alert(“无论msg”)作为JavaScript。我的UIWebView将弹出一个窗口并显示该警报消息。

Is there a way to disable this kind of popup window or JavaScript alert window?

有没有办法禁用这种弹出窗口或JavaScript警报窗口?

3 个解决方案

#1


19  

Add this after your web view has loaded its content

在Web视图加载其内容后添加此项

[MyWebView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"];

#2


12  

You can bind window.alert to another function. So:

您可以将window.alert绑定到另一个函数。所以:

window.alert = function() {
  //does nothing so effectively "disables" alert
};

Make sure you do this before you call any alerts. The neat thing about this is you can customize the way you display messages to the user. So you could override window.alert to log to the console (for debugging purposes) or you can render it on the page (with a lightbox or something similar).

确保在拨打任何警报之前执行此操作。关于这一点的好处是你可以自定义向用户显示消息的方式。因此,您可以覆盖window.alert以登录到控制台(用于调试目的),或者您可以在页面上呈现它(使用灯箱或类似的东西)。

#3


1  

Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show;.

由于UIWebView将所有Javascript警报转换为本机UIAlertViews,因此在本机端阻止它非常简单。查看UIAlertView.h,只有一种公共方法可以显示一个方便调用的警报: - (void)show;。

@interface UIAlertView (Blocker)
@end

#import "UIAlertView+Blocker.h"

@implementation UIAlertView (Blocker)

- (void)show {
   return;
}
@end

You can find the answer here: https://*.com/a/21698251/2377378

你可以在这里找到答案:https://*.com/a/21698251/2377378

#1


19  

Add this after your web view has loaded its content

在Web视图加载其内容后添加此项

[MyWebView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"];

#2


12  

You can bind window.alert to another function. So:

您可以将window.alert绑定到另一个函数。所以:

window.alert = function() {
  //does nothing so effectively "disables" alert
};

Make sure you do this before you call any alerts. The neat thing about this is you can customize the way you display messages to the user. So you could override window.alert to log to the console (for debugging purposes) or you can render it on the page (with a lightbox or something similar).

确保在拨打任何警报之前执行此操作。关于这一点的好处是你可以自定义向用户显示消息的方式。因此,您可以覆盖window.alert以登录到控制台(用于调试目的),或者您可以在页面上呈现它(使用灯箱或类似的东西)。

#3


1  

Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show;.

由于UIWebView将所有Javascript警报转换为本机UIAlertViews,因此在本机端阻止它非常简单。查看UIAlertView.h,只有一种公共方法可以显示一个方便调用的警报: - (void)show;。

@interface UIAlertView (Blocker)
@end

#import "UIAlertView+Blocker.h"

@implementation UIAlertView (Blocker)

- (void)show {
   return;
}
@end

You can find the answer here: https://*.com/a/21698251/2377378

你可以在这里找到答案:https://*.com/a/21698251/2377378