Why does the WebView html in my Mac app refuse to load JQuery or any external script

0 votes

I have a simple index.html file like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript">

      $(function() {

        setInterval(function(){

          if (typeof Cocoa !== 'undefined') {

            Cocoa.log('JQuery loaded...');

          }

        }, 3000);

      });

    </script>

  </head>
  <body>
    <h1>Testing script in objective c enviroment.</h1>
    <div id="container" class="col">
      <p>Paragraph 1</p>
      <p>content goes here</p>
    </div>
  </body>
</html>

loaded from a WebView subclass in my AppDelegate:

appDelegate.h

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property NSWindow *webWindow;
@property myWebView *myWV;

@end

// ...

appDelegate.m

#import <WebKit/WebKit.h>
#import "myWebView.h"

@implementation AppDelegate

// ...

- (void)applicationDidFinishLaunching:(NSNotification *)notification {


    CGRect webRect = CGRectMake(0, 0, 1000, 1000);

    self.webWindow = [[NSWindow alloc] initWithContentRect:webRect styleMask:NSTexturedBackgroundWindowMask backing:NSBackingStoreBuffered defer:NO];
    self.webWindow.contentView = [[NSView alloc] initWithFrame:webRect];

    self.myWV = [[myWebView alloc] initWithFrame:webRect frameName:@"myWV" groupName:@"webViews"];
    self.myWV.UIDelegate = self;
    self.myWV.resourceLoadDelegate = self;
    self.myWV.frameLoadDelegate = self;

    [self.webWindow.contentView addSubview:self.myWV];
    [self.webWindow orderFront:NSApp];

    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index"
                                                         ofType:@"html"
                                                    inDirectory:@""];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
    [[self.myWV mainFrame] loadRequest:request];

}

// associate js "Cocoa.log" function with -logJavaScriptString: method
+ (NSString *)webScriptNameForSelector:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return @"log";
    return nil;
}

//this allows JavaScript to call the -logJavaScriptString: method
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
    if(sel == @selector(logJavaScriptString:))
        return NO;
    return YES;
}

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSLog(@"webView loaded");
    [[self.myWV windowScriptObject] setValue:self forKey:@"Cocoa"];   
}

- (void)logJavaScriptString:(NSString *)logText
{
    NSLog(@"JavaScript: %@",logText);
}

Why does nothing inside $(function() {}); get called? If I place the interval log function outside the JQuery onReady function, it does start to call the correct method after a few seconds.

Is there some cross-site policy issue going on that's preventing scripts from being loaded? If so, how can I disable it? Why isn't jQuery loading?

Aug 5, 2022 in Web Development by gaurav
• 23,260 points
840 views

1 answer to this question.

0 votes

I changed

https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js

to

http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js

and now it works. If anyone knows why WebView doesn't like https, let me know in the comments.

answered Aug 5, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

how can i get the url of the content( home.html) in adress bar by jquery load() function?

I am using jquery load() function to ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
586 views
0 votes
1 answer

How to load external scripts dynamically in Angular?

Hello @kartik, You can use following technique to ...READ MORE

answered Sep 8, 2020 in Web Development by Niroj
• 82,880 points
5,372 views
0 votes
0 answers

How to add a link in Jquery PrettyPhoto to download the image

I am using Jquery PrettyPhoto to have ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
872 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
702 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,495 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
761 views
0 votes
1 answer

how to use substr() function in jquery?

To get substring of a string in ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,235 views
0 votes
1 answer

How to loop through array in jQuery?

Generic loop: var i; for (i = 0; i ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,680 points
2,199 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP