【问题标题】:stringByEvaluatingJavaScriptFromString doesn't fire JavaScriptstringByEvaluatingJavaScriptFromString 不会触发 JavaScript
【发布时间】:2011-09-10 23:23:06
【问题描述】:

当我在 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;
    }

【问题讨论】:

    标签: javascript objective-c cocoa webview


    【解决方案1】:

    默认情况下,WebView 不会显示 JavaScript 警报。实际上,您需要自己实现代码来显示 JavaScript 警报,方法是将对象设置为 WebView 的委托并实现

    ‑webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:

    委托方法。

    但是,在您的情况下,绝对没有理由这样做,因为您可以简单地创建一个标准 NSAlert。我不明白你为什么要让WebView 显示警报。为什么不直接显示呢?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      相关资源
      最近更新 更多