stringByEvaluatingJavaScriptFromString 不会触发 JavaScript

发布于 2024-12-04 05:48:58 字数 1684 浏览 0 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-12-11 05:48:58

默认情况下,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 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文