Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2009, 12:21 PM   #1 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,658
Default UIWebView loading a local html file using javascript

Hello All,

Just as a learning exercise I took a webpage that I have, which runs some javascript functions, and am trying to bring it into an UIWebView. For simplicity sake lets just say that i have two documents; an html document and a .js javascript file which it includes. On my original website I fill out a form and hit submit; which calls the javascript function to alter the page a bit based on the input.

Now I have brought these two files into my xCode project and loaded the html file locally into a UIWebView. However, when i hit submit; nothing happens. Is there something special I need to do to make the javascript work as it did on the original web version? Thanks.
scotopia is offline   Reply With Quote
Old 06-01-2009, 03:49 PM   #2 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,658
Default

Quote:
Originally Posted by scotopia View Post
Hello All,

Just as a learning exercise I took a webpage that I have, which runs some javascript functions, and am trying to bring it into an UIWebView. For simplicity sake lets just say that i have two documents; an html document and a .js javascript file which it includes. On my original website I fill out a form and hit submit; which calls the javascript function to alter the page a bit based on the input.

Now I have brought these two files into my xCode project and loaded the html file locally into a UIWebView. However, when i hit submit; nothing happens. Is there something special I need to do to make the javascript work as it did on the original web version? Thanks.

Any thoughts?
scotopia is offline   Reply With Quote
Old 06-01-2009, 08:21 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,569
Default

Quote:
Originally Posted by scotopia View Post
Any thoughts?
Fist make sure the javascript works when you drag the HTML directly into safari on your desktop machine. If it doesn't work there, you have a more basic problem, and it's easier to debug on the desktop.

Then, see if mobile safari is the problem. You can put your HTML on a server, enable errors in mobile safari (it's in the settings app) and see if you're getting a javascript error in your onsubmit function.

You could also implement webView:didFailLoadWithError: in your UIWebView's delegate - see if you're getting any errors there. That probably won't show any Javascript errors, but you'll probably get something there if your onsubmit function fails, and it tries to go to the (unspecified?) action of your form.

I don't know any way to debug in the UIWebview, other than the old fashioned way - adding alert() boxes at each step of the script to see how far you're getting. On the form submit, inside the function itself, etc.
__________________
smasher is offline   Reply With Quote
Old 06-02-2009, 01:30 PM   #4 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,658
Default

Quote:
Originally Posted by smasher View Post
Fist make sure the javascript works when you drag the HTML directly into safari on your desktop machine. If it doesn't work there, you have a more basic problem, and it's easier to debug on the desktop.

Then, see if mobile safari is the problem. You can put your HTML on a server, enable errors in mobile safari (it's in the settings app) and see if you're getting a javascript error in your onsubmit function.

You could also implement webView:didFailLoadWithError: in your UIWebView's delegate - see if you're getting any errors there. That probably won't show any Javascript errors, but you'll probably get something there if your onsubmit function fails, and it tries to go to the (unspecified?) action of your form.

I don't know any way to debug in the UIWebview, other than the old fashioned way - adding alert() boxes at each step of the script to see how far you're getting. On the form submit, inside the function itself, etc.
Thanks for the reply smasher.

It worked on my desktop; and I threw it up on a webserver and browsed to it using mobile safari and that worked as well...hmm.

Last edited by scotopia; 06-02-2009 at 01:32 PM.
scotopia is offline   Reply With Quote
Old 07-09-2009, 11:48 AM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,569
Default Loading javascript in a UIWebView

Ancient thread bump, for the benefit of googlers. May be the same problem as this thread:

Loading local .js file into uiwebview problem
__________________
smasher is offline   Reply With Quote
Old 07-09-2009, 12:12 PM   #6 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,658
Default

Quote:
Originally Posted by smasher View Post
Ancient thread bump, for the benefit of googlers. May be the same problem as this thread:

Loading local .js file into uiwebview problem
Thanks Smasher, I forgot I was trying to figure this out (got sidetracked by other things ). I don't remember getting warnings like this guy was getting; but it certainly seems like this could be my issue; I'll check it out when I get home from work and report back.
scotopia is offline   Reply With Quote
Old 07-11-2009, 06:41 PM   #7 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,658
Default

Quote:
Originally Posted by scotopia View Post
Thanks Smasher, I forgot I was trying to figure this out (got sidetracked by other things ). I don't remember getting warnings like this guy was getting; but it certainly seems like this could be my issue; I'll check it out when I get home from work and report back.
That was it Smasher; thanks again! Solution here: iPhone UIWebView local resources using Javascript and handling onorientationChange - Stack Overflow
scotopia is offline   Reply With Quote
Old 11-05-2009, 03:51 AM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 171
Smile How to do a search feature in PDF loaded in UIWebView?Plz help me

Quote:
Originally Posted by smasher View Post
Ancient thread bump, for the benefit of googlers. May be the same problem as this thread:

Loading local .js file into uiwebview problem
I have a pdf loaded onto a UIWebView. I want to do search of a string in that PDF. So I used these steps
Code:
	NSString* addressText =[NSString stringWithFormat:searchBar.text];
	NSString *javaSript=[NSString stringWithFormat:
						 @"var highlightStartTag = \"<font style='color:blue; background-color:yellow;'>\";"
						 " var highlightEndTag = \"</font>\";"
						 " var bodyText = document.body.innerHTML;"
						 " var searchTerm=%@;"
						 " var newText = \"\";"
						 " var i = -1;"
						 " var lcSearchTerm = searchTerm.toLowerCase();"
						 " var lcBodyText = bodyText.toLowerCase();"
 						 " while (bodyText.length > 0){"
						 " i = lcBodyText.indexOf(lcSearchTerm, i+1);"
						 " if (i < 0){"
						 " newText += bodyText;"
						 " bodyText = "";}"
						 " else{"
						 " if (bodyText.lastIndexOf(\">\", i) >= bodyText.lastIndexOf(\"<\", i)){"
						 "if (lcBodyText.lastIndexOf(\"/script>\", i) >= lcBodyText.lastIndexOf(\"<script\", i)){"
						 " newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;"
						 " bodyText = bodyText.substr(i + searchTerm.length);"
						 " lcBodyText = bodyText.toLowerCase();"
						 " i = -1;"
						 " }"
						 " }"
						 " }"
						 " }"
						 " bodyText = newText;"
						 " document.body.innerHTML = bodyText;",addressText];
	[webView stringByEvaluatingJavaScriptFromString:javaSript];
But it seems to have no effect. Dont this work on a pdf loaded onto a webView. I have been doing this thing for about 3 days. Plz give me some idea.
wolverine is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,208
Threads: 38,991
Posts: 171,023
Top Poster: smasher (2,569)
Welcome to our newest member, JDBurgie
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0