Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you enable javascript in a WebView
    text
    copied!<p>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:</p> <pre><code>- (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]; } </code></pre> <p>The index.html file contains the following:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Hello, World!&lt;/title&gt; &lt;link href="style.css" rel="stylesheet" type="text/css" /&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; jQuery(function($){ $('h1').fadeOut() }) &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello, World!&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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 <code>h1</code> 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.</p> <p>The code for this is available at <a href="http://github.com/pjb3/WebViewApp" rel="nofollow noreferrer">http://github.com/pjb3/WebViewApp</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload