JavaScript in UIWebView?

Discussion in 'iOS Programming' started by Danneman101, Dec 8, 2008.

  1. Danneman101
    Expand Collapse
    macrumors 6502

    Joined:
    Aug 14, 2008
    #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>
    
     
  2. ghayenga
    Expand Collapse
    macrumors regular

    Joined:
    Jun 18, 2008
    #2
    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.
     
  3. Danneman101
    Expand Collapse
    thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    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>
    

    What specifically does work? The code above with a functioncall - unaltered?
     
  4. ghayenga
    Expand Collapse
    macrumors regular

    Joined:
    Jun 18, 2008
    #4
    I have put javascript functions in the headers of the html and they execute just fine when loaded into the UIWebView.
     
  5. Danneman101
    Expand Collapse
    thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #5
    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.
     
  6. ghayenga
    Expand Collapse
    macrumors regular

    Joined:
    Jun 18, 2008
    #6
    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>
     
  7. Danneman101
    Expand Collapse
    thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #7
    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.
     
  8. detz
    Expand Collapse
    macrumors 65816

    Joined:
    Jun 29, 2007
    #8
    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.
     
  9. ghayenga
    Expand Collapse
    macrumors regular

    Joined:
    Jun 18, 2008
    #9
    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.
     
  10. Danneman101
    Expand Collapse
    thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #10
    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>
    
     

Share This Page