stringByEvaluatingJavaScriptFromString 不会触发 JavaScript
当我在 Web 视图中按下向上键时,它会记录它,但不会触发 stringByEvaluatingJavaScriptFromString
中定义的 JS 警报,有什么想法吗?谢谢
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[webView setMainFrameURL:@"http://google.com"];
[webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
}
-(id)init {
if( !(self = [super init]) ){
return nil;
}
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
switch ([theEvent keyCode]) {
case 123: // Left arrow
NSLog(@"Left behind.");
break;
case 124: // Right arrow
NSLog(@"Right as always!");
break;
case 125: // Down arrow
NSLog(@"Downward is Heavenward");
break;
case 126: // Up arrow
[webView stringByEvaluatingJavaScriptFromString:@"alert('yo')"];
NSLog(@"Up, up, and away!");
break;
default:
break;
}
// Return the event, a new event, or, to stop
// the event from being dispatched, nil
return theEvent;
};
// Creates an object we do not own, but must keep track
// of so that it can be "removed" when we're done
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
return self;
}
when I press the up key in my web view, it logs it but it doesn't fire the JS alert as defined in stringByEvaluatingJavaScriptFromString
, any ideas? Thanks
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[webView setMainFrameURL:@"http://google.com"];
[webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
}
-(id)init {
if( !(self = [super init]) ){
return nil;
}
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
switch ([theEvent keyCode]) {
case 123: // Left arrow
NSLog(@"Left behind.");
break;
case 124: // Right arrow
NSLog(@"Right as always!");
break;
case 125: // Down arrow
NSLog(@"Downward is Heavenward");
break;
case 126: // Up arrow
[webView stringByEvaluatingJavaScriptFromString:@"alert('yo')"];
NSLog(@"Up, up, and away!");
break;
default:
break;
}
// Return the event, a new event, or, to stop
// the event from being dispatched, nil
return theEvent;
};
// Creates an object we do not own, but must keep track
// of so that it can be "removed" when we're done
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,
WebView
不会显示 JavaScript 警报。事实上,您需要自己实现代码来显示 JavaScript 警报,方法是设置一个对象作为WebView
的委托并实现-webView:runJavaScriptAlertPanelWithMessage:initiedByFrame:
委托方法。
但是,就您而言,绝对没有理由这样做,因为您可以简单地创建一个标准
NSAlert
。我不明白您为什么要尝试让WebView
显示警报。为什么不直接显示呢?A
WebView
will not display JavaScript alerts by default. In fact, you need to implement the code to display JavaScript alerts yourself, by setting an object as the delegate of theWebView
and implementing the‑webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
delegate method.
However, in your case there's absolutely no reason to do this, since you can simply create a standard
NSAlert
. I don't understand why you're trying to make theWebView
display the alert. Why not just display it directly?