PDA

View Full Version : JavaScript in UIWebView?




Danneman101
Dec 8, 2008, 04:03 PM
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
----------------------------

<html><head>
<script type="text/javascript">
document.write("Hello Macworld!");
</script>
</body></html>



2. Alert-box. Does NOT work
--------------------------------

<html><head>
<script type="text/javascript">
alert("Hello Macworld!");
</script>
</body></html>



3. Function in header. Does NOT work
-------------------------------------------

<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
Dec 8, 2008, 08:58 PM
What are the limitations to JavaScript in a UIWebView?

3. Function in header. Does NOT work
-------------------------------------------

<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
Dec 9, 2008, 06:00 AM
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.


<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
Dec 9, 2008, 06:49 PM
I have put javascript functions in the headers of the html and they execute just fine when loaded into the UIWebView.

Danneman101
Dec 10, 2008, 06:16 AM
In other words, this code will work just fine for you:


<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
Dec 10, 2008, 03:42 PM
In other words, this code will work just fine for you:


<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:

<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
Dec 13, 2008, 07:28 AM
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
Dec 13, 2008, 07:43 AM
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
Dec 13, 2008, 11:12 AM
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
Jan 3, 2009, 11:10 AM
Ive updated to sdk 2.2, and this code now works fine now. Thanks for the 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?


<html><head>
<script type="text/javascript">
alert("Hello Macworld!");
</script>
</body></html>