Setting innerHTML on iPhone

a 夏天 提交于 2020-02-02 13:19:12

问题


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/8d826c8506377025?lnk=gst&q=innerHTML#8d826c8506377025


回答1:


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.



来源:https://stackoverflow.com/questions/4223705/setting-innerhtml-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!