簡體   English   中英

stringByEvaluatingJavaScriptFromString不會觸發JavaScript

[英]stringByEvaluatingJavaScriptFromString doesn't fire 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;
    }

默認情況下, WebView不會顯示JavaScript警報。 實際上,您需要通過將一個對象設置為WebView的委托並實施代碼來實現自己顯示JavaScript警報的代碼。

‑webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:

委托方法。

但是,對於您而言,絕對沒有理由這樣做,因為您可以簡單地創建標准NSAlert 我不明白您為什么要使WebView顯示警報。 為什么不直接顯示呢?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM