Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 272831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:21:46+00:00 2026-05-12T00:21:46+00:00

I have a very simple Mac Cocoa app that just has a Web View

  • 0

I have a very simple Mac Cocoa app that just has a Web View that loads an HTML file with some CSS and JavaScript. I have hooked up the app delegate to this method:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  NSString* filePath = [[NSBundle mainBundle] pathForResource: @"index" ofType: @"html"];
  NSURL *url = [NSURL URLWithString: filePath];
  [[self.webView mainFrame] loadHTMLString: [NSString stringWithContentsOfFile: filePath] baseURL: url];
}

The index.html file contains the following:

<html>
  <head>
    <title>Hello, World!</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      jQuery(function($){
        $('h1').fadeOut()
      })
    </script>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

The style.css file gets loaded correctly, because I set the background to grey in that, so I know it can load other assets from the resource bundle. The javascript code does not get executed. If I open index.html in Safari, the Hello World h1 disappears, as expected. Why doesn’t this happen in the app in my WebView? Do I have to do something to enable JavaScript? The box next to JavaScript is checked in Interface Builder.

The code for this is available at http://github.com/pjb3/WebViewApp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:21:46+00:00Added an answer on May 12, 2026 at 12:21 am

    You don’t have to do anything to enable JavaScript; it’s enabled in the WebView by default. And like you said, you can always check in Interface Builder that the JavaScript checkbox is selected.

    What you have to do, though, is make sure that the baseURL you’re specifying is correct (since in the html file you’re referring to jquery.js with a relative URL using just the filename) and that you’ve included the jquery.js file into the bundle resources.

    The code you posted will result in a nil value for the url variable: +URLWithString: expects a valid URL string (that conforms to RFC 2396) as the argument (which the filesystem path you’re providing is not). What you want is +fileURLWithPath:, and you also need to use the filesystem path of the directory where index.html and jquery.js reside, which you can get by removing the filename from the path you’ve got (using NSString‘s -stringByDeletingLastPathComponent:). Here’s how you get the correct baseURL:

    NSURL *url = [NSURL fileURLWithPath:[filePath stringByDeletingLastPathComponent]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have a very simple bit of script that changes the status of an
I have a very simple Java RMI Server that looks like the following: import
I have a very simple ASP.Net page that acts as a front end for
I have some very simple code to generate an assembly and invoke a method
I have a very simple .htaccess file set up to redirect a 404 to
I have a very simple mod_wsgi python application that tries to write to a
I just made a very simple website, but I have run into a problem.
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple problem which requires a very quick and simple solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.