Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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>
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
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.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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>


By the way, it does.

What specifically does work? The code above with a functioncall - unaltered?
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
I have put javascript functions in the headers of the html and they execute just fine when loaded into the UIWebView.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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.
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
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>
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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.
 

detz

macrumors 65816
Jun 29, 2007
1,051
0
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.
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
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.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.