开发者

Setting innerHTML on iPhone

开发者 https://www.devze.com 2023-01-26 09:34 出处:网络
I am setting the innerHTML property of document element within this way: document.getElementById(\'some_element\').innerHTML = newHTML;

I am setting the innerHTML property of document element within this way:

document.getElementById('some_element').innerHTML = newHTML;

And this works fine in web browser on desktop and on iPhone simulator. But on iPhone device this works good randomly. I mean, sometimes works, sometimes does not. One proposed solution from google groups is to try to repeat this function call about 20 times with 50 ms pause between. Come on. Is there some logical explanation about that mysterious delay?

Here are the links for this issue on google groups:

http://groups.google.com/group/phonegap/browse_thread/thread/0d24a9fda0921407#

http://groups.google.com/group/phonegap/browse_thread/thread/a5787985293b6de1/开发者_StackOverflow中文版8d826c8506377025?lnk=gst&q=innerHTML#8d826c8506377025


When is that code called?

I guess if this code is called when the document is loadead, like onLoad(), the DOM might not have loaded completely yet.

Personally for these kind of things I simply include jQuery and do something like this:

<script type="text/javascript" src="jquery-1.4.3.min.js"></script>

<script type="text/javascript">
  $(document).ready( function() {
      $('#some_element').html(newHTML);
  } );
</script>

... or in Prototype:

document.observe('dom:loaded', function(){
    // do yo thang...
});

This ensures that the DOM is completely loaded before the script fires; thus avoiding this kind of race condition.

To include jQuery, just download it from their official website and drop it in your Xcode project. Alternatively, if your HTML document is served via HTTP, you can just drop it next to the HTML file on the server.

Oh, and before I forget it. The code that loads my HTML file from the project looks like this:

NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"myHtmlFile" 
                                         withExtension:@"html"];

NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];

[webView loadRequest:request];

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号