Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView doesn't load external Javascript file

I have the following innocent-looking code (index.html), but it doesn't load the code.js file. It works well if I copy/paste the contents of the file in the HTML page. Both index.html and code.js files are in the same directory. This is the code snippet. Any help would be great.

index.html :

<html> <head> <script type="text/javascript" src="code.js"></script> </head>  <body onload="drawImage()">   <div><canvas id="canvas" width="320" height="480"></canvas></div>  </body> </html> 

ViewController.m:

- (void)viewDidLoad {   [super viewDidLoad]; // load the first webpage [homePageWebView loadRequest:[NSURLRequest requestWithURL:[NSURLfileURLWithPath:   [[NSBundle mainBundle] pathForResource:@"index"ofType:@"html"] isDirectory:NO]]]; } 
like image 733
Amarsh Avatar asked Aug 31 '10 05:08

Amarsh


People also ask

Why is my external JavaScript not working?

You should put all javascript code in a <script> tag for the browser to recognize and make it run and for the src attribute of the script tag to find your 'script. js' file, it just has to be in the same directory as the html file, you don't need to specify the entire file root.

How do I include an external JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can JavaScript be external?

The programs in JavaScript are called scripts which are executed as plain texts. We can write these scripts directly on our HTML page or use an external JavaScript file. JavaScript can execute in the browser, and also on the server, or actually on any device that has a special program called the JavaScript engine.


2 Answers

I have found that Xcode thinks that the js file needs to be compiled.

Open the project in Xcode. Go to "Targets" and look in "Compile Sources". If you see your js file in that folder, move it to the "Copy Bundle Resources" folder and delete it from the "Compile Sources" folder.

Delete the app from your iPhone if it is installed already for testing. Go to the Build menu in Xcode and Clean all Targets. Then recompile.

Check now to see if your HTML file actually sees your js file now.

Linda

like image 129
Linda Avatar answered Sep 20 '22 08:09

Linda


You need to read the html file into a string and set the baseURL when loading so relative paths can be understood.

NSError* error; NSString* html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] encoding:NSASCIIStringEncoding error:&error];  [webview loadHTMLString:html baseURL:[self getBaseURL]];  - (NSURL*) getBaseURL {     NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];     NSRange r = [path rangeOfString:@"/" options:NSBackwardsSearch];     path = [path substringToIndex:r.location];      path = [path stringByReplacingOccurrencesOfString:@"/" withString:@"//"];     path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];     path = [NSString stringWithFormat:@"file:/%@//",path];     return  [NSURL URLWithString:path]; } 

Something like that should work for you.

like image 39
skorulis Avatar answered Sep 23 '22 08:09

skorulis