Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mohit Athwani
3,540 PointsCalling JS from ObjC
I want to execute certain JS functions on a webView.
Everything works fine if my JS file has only function. But if i put in another function and try to call it at some other point, it won't work. How can I fix this ?
My code is :
[self.webView loadHTMLString:@"<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script><script src=\"signup.js\"></script>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
NSString *function1 = [[NSString alloc] initWithFormat: @"func1()"];
NSString *function2 = [[NSString alloc] initWithFormat: @"func2()"];
NSString *result1 = [self.webView stringByEvaluatingJavaScriptFromString:function1];
NSString *result2 = [self.webView stringByEvaluatingJavaScriptFromString:function2];
How can i fix this ?
5 Answers
Amit Bijlani
Treehouse Guest Teacher@Mohit that should work. What's in "signup.js"? And what do the functions do?
Mohit Athwani
3,540 PointsI'm using the signup.js to send an AJAX POST to a ruby server.
signup.js has two functions :
var signup = function(email, password, fullname) {
//jQuery AJAX POST done here
};
and
var login = function (email,password) {
// Log the user in after successful sign up
};
signup() works perfectly ... login() is never called ... login() only has 2 lines as of now :
alert("Hello !");
return 1;
for me to check if control actually ever executes login()
Amit Bijlani
Treehouse Guest Teacher@Mohit It could be that the login takes too long to execute. Please refer to the documentation for stringByEvaluatingJavaScriptFromString it states:
JavaScript execution time is limited to 10 seconds for each top-level entry point. If your script executes for more than 10 seconds, the web view stops executing the script. This is likely to occur at a random place in your code, so unintended consequences may result. This limit is imposed because JavaScript execution may cause the main thread to block, so when scripts are running, the user is not able to interact with the webpage.
Mohit Athwani
3,540 PointsYep, I've read about the 10 second rule. I think you even mentioned it in the Deep Dive video for WebViews
login() only has 2 lines as of now :
alert("Hello !");
return 1;
for me to check if control actually ever executes login()
Amit Bijlani
Treehouse Guest TeacherProbably the page hasn't finished loading. Try calling the functions in the delegate method of - (void)webViewDidFinishLoad:(UIWebView *)webView.