2 Replies Latest reply: Mar 11, 2013 6:28 AM by user12956197 RSS

    how to get corresponding org.w3c.dom.Element of html element?

    user12956197
      I use javascript to track which elements are clicked by user in webview.
      How can I get the correspond org.w3c.dom.Element in webengine?

      <javascript>
      //global javascript object
      selectedElems = ....;

      </javascript>


      JSObject obj=webengine.executeScript("selectedElem");

      Element elem=engine.getDocument().getDocumentElement();

      how can I find the elem's offspring which is corresponding to selectedElem?

      I tried to use selectedElem.setUserData("key","uniqValue",null); but

      it's only supported in dom level 3 in firefox and will be removed in dom level 4.

      http://stackoverflow.com/questions/432174/how-to-store-arbitrary-data-for-some-html-tags
        • 1. Re: how to get corresponding org.w3c.dom.Element of html element?
          peterz
          Just cast [obj] to Element. When executeScript() evaluates to a DOM node, it returns a JSObject that is also an instance of org.w3c.dom.Node. See WebEngine's javadoc for some more details.
          • 2. Re: how to get corresponding org.w3c.dom.Element of html element?
            user12956197
            thanks. but maybe I don't describe my problem well.

            I use javascript to get some information such as position, sytle of each elements and then pass these to javafx. So the besides the dom tree, I can get more information about elements.
            I need to pass them back to javafx.

            if node support setUserData, I can associate them to each Node/Element. But webkit don't support this feature.

            BTW, If javascript pass a object to javafx. it can only deal with primary types such as string, numeric. But I need pass back a complicated javascript object like:

            {name:"parent", children:[{name:"child1"},{name:"child2"}]
            I can use (String)JSObject.getMember("name"); and JSObject children=JSObject.getMember("children");
            but I can't cast children to Object[] or JSObject[].
            so I have to use some tricky to convert Javascript Object to json string and then in javafx convert it back to Object tree.

            Edited by: user12956197 on 2013-3-11 上午4:27