0

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

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • You don't need `javascript:` prefix. That error looks weird. But I think you can solve it in the js side by adding this to the first line of your function: `moduleElementId = moduleElementId.id` since it's a dom element. – Yunus Emre May 14 '20 at 06:46
  • thanks @YunusEmre. trying to see if there is explanation why the passed in string id automatically resolved into the Element with that id. – lannyf May 14 '20 at 11:56

0 Answers0