" /> " /> "/>

android webview javascript bridge, why passed in string got resolved to the div element

280 Views Asked by At

In webview code it calls into the loaded javascript, passing in the string for the html div element id `"module_1", <div id="module_1" class="module"></div>:

my_web_view.evaluateJavascript(
     "javascript:sendHtmlMarkerLocation("module_1");", null)

and the javascript is to accepting a string for the div element id, and then lookup the div element and get its location:

        function sendHtmlMarkerLocation( moduleElementId ) {

            console.log('+++ enter sendHtmlMarkerLocation()'+moduleElementId+', 
                         window.androidObj:'+window.androidObj);

            var elm = null
            var moduleId = moduleElementId
            if (moduleElementId instanceof String) {
                elm = document.getElementById(moduleElementId)
            } else if (moduleElementId instanceof Element) { // how it changes to the div element ???
                elm = moduleElementId 
                moduleId = elm.id
            }

            var _x = 0;
            var _y = 0;
            while( elm && !isNaN( elm.offsetLeft ) && !isNaN( elm.offsetTop ) ) {
                _x += elm.offsetLeft - elm.scrollLeft;
                _y += elm.offsetTop - elm.scrollTop;
                elm = elm.offsetParent;
            }
            var width = getWindowWidth()
            var height = getWindowHeight()
            window.androidObj.sendLocationToAndroid(moduleId, _x, _y, width, height);
        }

but it crashes since the passed in moduleElementId is not the string div id "module_1" anymore, instead it is resolved to the <div id="module_1" class="module"></div>.

the coresponding console.log is:

+++ enter sendHtmlMarkerLocation()[object HTMLDivElement], window.androidObj:function AndroidClass(){} @ 48

How it is changed from string id to the <div> element?

enter image description here

0

There are 0 best solutions below