Senthilkumar
Senthilkumar

Reputation: 2481

How to determinate stringByEvaluatingJavaScriptFromString evaluation porcess complete?

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,

Upvotes: 0

Views: 514

Answers (2)

user3035961
user3035961

Reputation: 66

"stringByEvaluatingJavaScriptFromString" is an synchronous function, once the javascript completes excution then the callback is returned.

However you can make it asynchronous by making the javascript to run asynchronously

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

In Javascript write a function to

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

Using this your native thread will be empty and will get return immediately with in 3-4 msec

Upvotes: 1

carlossless
carlossless

Reputation: 1181

The function is not asynchronous. It finishes right before returning a value from the evaluation function.

Upvotes: 1

Related Questions