Android webview SKIPS javascript even with setJavascriptEnabled(true) and WebChromeClient

前端 未结 4 1133
清歌不尽
清歌不尽 2020-12-18 01:02

(using Samsung Galaxy Tab and Android 3.0, this is intended to work on every 3.0+ tablet, like it does in their browsers, just not webview)

I have a page with CSS an

4条回答
  •  忘掉有多难
    2020-12-18 01:24

    I encountered the same problem quite some time ago - my final solution was to ditch jquery and just use plain JS. Things were constantly breaking while they worked fine when viewed from the normal Android-browser.

    If those are the only lines that break the code, try reproducing the same functionality of those lines with pure JS. The Android's native webview has a pretty good support for all the latest JS goodies,

    so...

    $('body').css({height : height+'px', width : Math.floor(width*0.98)+'px' });
    $('.text').css({height : height+'px', width : Math.floor(width*0.98)+'px' }); 
    $('#firstpage').css({height : height+'px', width : Math.floor(width*0.98)+'px' }); 
    $('body').prepend(''); 
    

    would be...

    var doc = document,
        body = doc.getElementsByTagName('body')[0],
        bodyStyle = body.style,
        firstPage = doc.getElementById('firstpage'),
        firstPageStyle = firstPage.style,
    
        head = doc.getElementsByTagName('head')[0],
        style = doc.createElement('style'),
        rules = doc.createTextNode('.text{-webkit-column-count:'+numCols+';}');
    
    bodyStyle.height = height + 'px';
    bodyStyle.width = Math.floor(width * 0.98) + 'px';
    
    firstPageStyle.height = height + 'px';
    bodyStyle.width = Math.floor(width * 0.98) + 'px';
    
    style.type = 'text/css';
    
    if( style.styleSheet )
        style.styleSheet.cssText = rules.nodeValue;
    else 
        style.appendChild( rules );
    
    head.appendChild( style );
    

    Just make sure that the above code runs after all the elements used have loaded (after DOM ready state, or before the closing of the tag )

    ps. Also since you're using Android's webkit - why is there need for -moz-column-count?

提交回复
热议问题