Advertisement
Guest User

phonegap iphone innerHTML hack (when using sqlite)

a guest
Jul 21st, 2010
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function safeInnerHTML(targetId, contentToSet, count){
  2.     // iPhone sometimes just fails to set innerHTML - no idea why. you end up with an empty div.
  3.     // it's more reliable with a setTimeout but still not reliable enough.
  4.     // this function sets the text and then checks it. if it's not there, it tries once more. horrible, but necessary.
  5.     // note: this really became an issue within the app and was even worse in 1st gen and 3g. 3GS was mostly fixed with one timeout, whereas even 3 didn't seem to always fix pre-3GS
  6.     // some blog posts indicated that they noticed the problem only when the messed with location.href (http://blog.johnmckerrell.com/2007/03/07/problems-with-safari-and-innerhtml/)
  7.     // so i've removed this stuff in the app and location.href is no longer changed. seems to be worse with database than it was with XHR but assume we'll leave it in place to be safe
  8.     var target = document.getElementById(targetId);
  9.     target.innerHTML = contentToSet;
  10.     var timeout = 50;
  11.     if (!count){
  12.         count = 1;
  13.         myLog("setting " + targetId + ".innerHTML with " + contentToSet.length + " chars.");
  14.     } else if (count > 29){
  15.         timeout = 200;
  16.     } else if (count > 19){
  17.         timeout = 100;
  18.     }
  19.    
  20.     if (contentToSet != '' && target.innerHTML == '' && count < 40){
  21.         myLog(targetId + ".innerHTML fail: " + count + " ( + " + timeout + "ms timout)");
  22.         ++count;
  23.         setTimeout( function() {
  24.             safeInnerHTML(targetId, contentToSet, count);
  25.         }, timeout);
  26.     } else if (count == 40){
  27.         alert2("Sorry, an error occurred. Please click the 'Home' tab button and try again");
  28.         if (mobConfig.inPhoneGap && mobConfig.ui.flurryOn && navigator.standalone != undefined){
  29.             var mobHistoryLen = mobHistory.length;
  30.             var args = {};
  31.             args.count = 40;
  32.             args.target = targetId;
  33.             args.previously = mobHistory[mobHistoryLen-1];
  34.             PhoneGap.exec("myFlurry.logSafeInner", args);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement