9

I have a iOS app to load a html string which refer to external css file and javascript file in the same directory.

Here is my html file:

<html>
<head>
    <meta name="viewport" content= user-scalable="no", width="device-width" />
    <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
    <script type="text/javascript" src="myjavascript.js"></script>
</head>

The css file works fine but it seems the js file never loaded. What could be the reason?

By the way I'm using loadHtmlString: BaseUrl method to load my html string.

Thanks

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • 1
    Refer [this](http://stackoverflow.com/questions/7012537/sourcing-local-javascript-in-uiwebview-ios?rq=1) link and hope its helpful as i have not tried that. – Paresh Navadiya Jun 28 '12 at 02:22
  • Can you show your iOS code for this? – nhahtdh Jun 28 '12 at 02:28
  • 3
    Check to make sure that the javascript is in your *Copy Bundle Resources* section and not in the *Compile Sources* section. I don't know why but sometimes Xcode thinks it should compile your Javascript. Of course, it can't, so you just wind up with a bunch of no-op code and no resource for your HTML. – borrrden Jun 28 '12 at 03:22
  • Thanks for your reply. I end up without using embedded javascript. But I'll check your suggestion works or not. – Bagusflyer Jul 17 '12 at 04:51
  • It does work. The two unaccepted answers which discuss moving .js files from "Compile Resources" to "Copy Bundle Resources" are correct; this fixed the issue for me. – Glenn Barnett Apr 02 '13 at 20:43

3 Answers3

7

This question is a bit old, but since it has not been answered, I would like to point out that if you would like your web view to load a javascript file residing in your bundle, the way to do it is:

[webView loadHTMLString:html
               baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

If you don't specify the baseURL there is no chance that the javascript file is loaded.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • This is exactly what I did. The problem is the javascript is still not loaded although the css is fine. – Bagusflyer Aug 17 '12 at 01:43
  • I see... are you sure that your javascript does not contain some errors? the issue with JS is that it fails silently. as a very raw debugging, you could put some alert statements in it (say, the first line in your js file), and see if they pop up... – sergio Aug 17 '12 at 06:44
  • Yes, I've test that by only add one line of JS code to alert.But it doesn't work. The CSS works. Thanks anyway. – Bagusflyer Sep 07 '12 at 00:13
5

By default, xcode will put new Javascript files in Compile Resources instead of Bundle Resources. Click on your project, then on your target. Under 'Build Phases' you may see the Javascript files under 'Compile Sources'. Drag them to down to 'Copy Bundle Resources'.

Kevin Borders
  • 2,933
  • 27
  • 32
  • 1
    This is exactly what happened to me when I dragged a few JS files into my project. I removed them from **Compile Resources** because Xcode was throwing an error, but without them being in **Bundle Resources**, they weren't available to my application. Thanks! – Clifton Labrum Mar 08 '14 at 00:19
  • This saved my life! Thanks @Kevin Borders! – gilsaints88 Mar 11 '14 at 07:56
2

like the others have said

  • use baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

  • copy Bundle Resources and not in Compile Resources.

thats it!

angel of code
  • 686
  • 8
  • 25