0

I'm reading the html file into the UIWebview. In the webViewDidFinishLoad I'm doing javascript function for horizontal scroll. Below code works fine. But i cant predict when javascript function finish.

in the visible webview first load the html file then script run. so i can determinate javascript finishing in the webViewDidFinishLoad.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    NSString *scriptString =@"javascript:function initialize() {  var eelam = document.getElementsByTagName('body')[0]; var ourH = window.innerHeight;var ourW = window.innerWidth;  var fullH = eelam.offsetHeight;  var pageCount = Math.ceil(fullH/ourH)+1; var padval = parseInt((screen.width*10)/100);var currentPage = 0; var newW = pageCount*ourW;eelam.style.height = (ourH - 30)+'px';eelam.style.width = (newW)+'px';eelam.style.webkitColumnGap = '5px'; eelam.style.margin = '25px'; eelam.style['margin-left']='2px';eelam.style.webkitColumnCount = pageCount; window.interface.setPageCount(pageCount);window.interface.setColumnWidth(screen.width);window.interface.setContent(eelam);window.interface.setColumnGap(padval);};";

    // set font
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
                          textFontSize];

    [mywebview stringByEvaluatingJavaScriptFromString:scriptString];

    [mywebview stringByEvaluatingJavaScriptFromString:@"javascript:initialize()"];

    [mywebview stringByEvaluatingJavaScriptFromString:jsString];

    // scroll disable
    UIView* row = nil;
    for(row in webView.subviews){
        if([row isKindOfClass:[UIScrollView class] ]){
            UIScrollView* scrollRow = (UIScrollView*) row;
            scrollRow.scrollEnabled = NO;
            scrollRow.bounces = NO;
            scrollRow.pagingEnabled=YES;
        }
    }

// this code is also not use to handle my issue
if ([[mywebview stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {

       }


}

please advise me,

thanks in advance,

4

2 回答 2

1

该函数不是异步的。它在从评估函数返回值之前完成。

于 2013-08-08T12:44:34.517 回答
1

"stringByEvaluatingJavaScriptFromString" 是一个同步函数,一旦 javascript 完成执行,就会返回回调。

但是,您可以通过使 javascript 异步运行来使其异步

示例:[mywebview stringByEvaluatingJavaScriptFromString:@"javascript:initialize()"];

在 Javascript 中编写一个函数

function initialize(){ setTimeout(function(){ //init},0);

使用这个你的本地线程将是空的,并会在 3-4 毫秒内立即返回

于 2014-03-03T10:19:59.380 回答