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 w开发者_如何转开发e're done
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
return self;
}
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 the WebView
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 the WebView
display the alert. Why not just display it directly?
精彩评论