views:

771

answers:

1

Running into the strangest problem on iPhone using jQuery with my WebViewController.

I have a div into which I append content:

<div id="thumbnails">
  Here are your thumbnails:
  <div id="mythumbs"></div>
</div>

The code looks like:

 for (var i=0; i < thumbs.length; i++) {
  var item = thumbs[i];
  $('<img class="imgthumb" />').data('url',item.Url).attr({
   "src": item.Thumbnail.Url,
  }).appendTo( $('#mythumbs') );
 };

Works great.

Then I switch to other divs ( $('#thumbnails').hide(); $('#someotherdiv').show() ), go on about my business, and eventually switch back to the thumbnails div.

At this point I can't seem to append any content to that div using jQuery anymore. I can remove just fine, but the append no longer works.

The exact same code works great on Firefox and Safari outside of the iPhone, but once in the embedded WebKit it fails.

If I modify the dom directly using javascript it works, but if I use jQuery it fails:

  var x = document.createTextNode('THE FIRST THING');
  document.getElementById('thumbspage').appendChild(x);
  $('#thumbspage').append('-- THE SECOND THING');

"THE FIRST THING" shows up but "THE SECOND THING" doesn't.

Any ideas? This thing is driving me nuts.

A: 

Finally found a work-around:

I was calling my objective-c function using window.location . It turned out if I added a setTimeout around the call to window.location everything started working again. I'm guessing calling window.location would immediately call the objective-C code, interrupting the javascript thread, leaving things in a bad state. By adding the setTimeout it allows the javascript thread to finish before jumping into the objective-C code. Or something like that.

Parand