JavaScript in UIWebView? - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 8, 2008, 04:03 PM   #1
Danneman101
macrumors 6502
 
Join Date: Aug 2008
JavaScript in UIWebView?

What are the limitations to JavaScript in a UIWebView?

Ive tried a couple of simple scripts to get the hang of what goes and what doesnt, and here are my results:


1. Write text. Works fine
----------------------------
Code:
<html><head>
<script type="text/javascript">
   document.write("Hello Macworld!");
</script>
</body></html>

2. Alert-box. Does NOT work
--------------------------------
Code:
<html><head>
<script type="text/javascript">
   alert("Hello Macworld!");
</script>
</body></html>

3. Function in header. Does NOT work
-------------------------------------------
Code:
<html><head>
<script type="text/javascript">
function message()
{
   alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body></html>
Danneman101 is offline   0 Reply With Quote
Old Dec 8, 2008, 08:58 PM   #2
ghayenga
macrumors regular
 
Join Date: Jun 2008
Quote:
Originally Posted by Danneman101 View Post
What are the limitations to JavaScript in a UIWebView?

3. Function in header. Does NOT work
-------------------------------------------
Code:
<html><head>
<script type="text/javascript">
function message()
{
   alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body></html>
If you already know the alertbox doesn't work why would you think calling an alertbox from a function would be a valid test of wether a function in a header *does* work.

By the way, it does.
ghayenga is offline   0 Reply With Quote
Old Dec 9, 2008, 06:00 AM   #3
Danneman101
Thread Starter
macrumors 6502
 
Join Date: Aug 2008
Quote:
Originally Posted by ghayenga View Post
If you already know the alertbox doesn't work why would you think calling an alertbox from a function would be a valid test of wether a function in a header *does* work.
Lol, yes, I can see how that would be confusing But Ive tried the function with a code that does work (document.write), and the function-call still does not execute.

Code:
<html><head>
<script type="text/javascript">
function message()
{
   document.write("Hello dude!");
}
</script>
</head>
<body onload="message()">
</body></html>

Quote:
Originally Posted by ghayenga View Post
By the way, it does.
What specifically does work? The code above with a functioncall - unaltered?
Danneman101 is offline   0 Reply With Quote
Old Dec 9, 2008, 06:49 PM   #4
ghayenga
macrumors regular
 
Join Date: Jun 2008
I have put javascript functions in the headers of the html and they execute just fine when loaded into the UIWebView.
ghayenga is offline   0 Reply With Quote
Old Dec 10, 2008, 06:16 AM   #5
Danneman101
Thread Starter
macrumors 6502
 
Join Date: Aug 2008
In other words, this code will work just fine for you:

Code:
<html>
<head>
<script type="text/javascript">
function message()
{
   document.write("Hello dude!");
}
</script>
</head>

<body onload="message()">
</body>
</html>
Strange, perhaps its a matter of me loading the html-page locally instead of from a url...? Ill try it out and see what happens.
Danneman101 is offline   0 Reply With Quote
Old Dec 10, 2008, 03:42 PM   #6
ghayenga
macrumors regular
 
Join Date: Jun 2008
Quote:
Originally Posted by Danneman101 View Post
In other words, this code will work just fine for you:

Code:
<html>
<head>
<script type="text/javascript">
function message()
{
   document.write("Hello dude!");
}
</script>
</head>

<body onload="message()">
</body>
</html>
Strange, perhaps its a matter of me loading the html-page locally instead of from a url...? Ill try it out and see what happens.
No, but if I save this as a text file and open it with my Mac Safari browser it doesn't work either.

A little messing about shows that Document.write doesn't work. Try this instead:

Code:
<html>
<head>
<script type="text/javascript">

function message()
{
	var theBody = document.getElementsByTagName('body')[0];
	
	theBody.innerHTML = "Hello dude!";
  // document.write("Hello dude!");
}

</script>
</head>

<body onload="message()">
</body>
</html>
ghayenga is offline   0 Reply With Quote
Old Dec 13, 2008, 07:28 AM   #7
Danneman101
Thread Starter
macrumors 6502
 
Join Date: Aug 2008
No, that didnt work either :/

The html-code is altered inside xcode, and saved as a .html-page. So it shouldnt be a problem with it being a text-document.


What about developing the actual html-structure in Dashcode instead, and then import them (inlcuding the images and other resources such as sound) into the xcode-project? Would that work better? Im asking rather than trying it out first since I have never done anything in Dashcode and it would take some investment of time getting a simple example up and running.
Danneman101 is offline   0 Reply With Quote
Old Dec 13, 2008, 07:43 AM   #8
detz
macrumors 65816
 
Join Date: Jun 2007
I've done this in a few apps...always works for me. Post up the entire code from reading to displaying. You could also try using the executeJS function provided by UIWebView.
detz is offline   0 Reply With Quote
Old Dec 13, 2008, 11:12 AM   #9
ghayenga
macrumors regular
 
Join Date: Jun 2008
Quote:
Originally Posted by Danneman101 View Post
No, that didnt work either :/

The html-code is altered inside xcode, and saved as a .html-page. So it shouldnt be a problem with it being a text-document.


What about developing the actual html-structure in Dashcode instead, and then import them (inlcuding the images and other resources such as sound) into the xcode-project? Would that work better? Im asking rather than trying it out first since I have never done anything in Dashcode and it would take some investment of time getting a simple example up and running.
I'm not sure what it is that you're doing with it, but it it works just fine on *my* XCode project, and on my iPhone.
ghayenga is offline   0 Reply With Quote
Old Jan 3, 2009, 11:10 AM   #10
Danneman101
Thread Starter
macrumors 6502
 
Join Date: Aug 2008
Ive updated to sdk 2.2, and this code now works fine now. Thanks for the code

Code:
<html>
<head>
<script type="text/javascript">

function message()
{
	var theBody = document.getElementsByTagName('body')[0];
	theBody.innerHTML = "Hello dude!";
}

</script>
</head>

<body onload="message()">
</body>
</html>
However, I cant get the alert-box code to work. Any suggestions on that one?

Code:
<html><head>
<script type="text/javascript">
   alert("Hello Macworld!");
</script>
</body></html>
Danneman101 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
UIWebview background color DennisBlah iPhone/iPad Programming 0 May 24, 2014 11:26 AM
Closing a UIWebView? Tander iPhone/iPad Programming 6 Jan 2, 2014 06:32 AM
Resolved: Using Javascript on UIWebView to get all of a particular div class troop231 iPhone/iPad Programming 9 Apr 3, 2013 07:50 AM
Nitro Engine in UIWebView DSchwartz88 Jailbreaks and iOS Hacks 3 Jun 28, 2012 09:16 PM
UIWebView Help drewgarcia23 iPhone/iPad Programming 1 Jun 5, 2012 09:50 AM

Forum Jump

All times are GMT -5. The time now is 08:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC