0

Is there any solution on how to determine object's position within DOM structure? Normally I'd use XPath but document.evaluate() is not cross-browser compatible so I'm throwing that off.

Sample page HTML:

<html>
    <head></head>
    <body>
        <div>Some element 1</div>
        <div>Some element 2</div>
        <div>
            <p>some text 1</p>
            <img src="#">
            <p>some text 2</p>
        </div>
    </body>
</html>

Now imagine user selects text "text 2" within second paragraph. What I need is to get something like /html/body/div[3]/p[2][.='text 2'] and save it via $.ajax() in database. When user enters the same page again (somewhere in future) I need to highlight text "text 2" again and scroll the second paragraph into view (probably using window.scrollTo()).

MaRmAR
  • 136
  • 3
  • 12
  • 1
    I think [this questions is very similar](http://stackoverflow.com/questions/3454526/how-to-calculate-the-xpath-position-of-an-element-using-javascript), maybe its answer gives you some ideas: – Pablo Lozano Oct 08 '14 at 15:08
  • @Pablo Thanks, that could really help for getting XPath! But what about reverse mode? I need to get DOM element based on this XPath. – MaRmAR Oct 08 '14 at 16:11
  • Found most complete code here: http://stackoverflow.com/questions/2661818/javascript-get-xpath-of-a-node/5178132#5178132 – MaRmAR Oct 08 '14 at 16:18

0 Answers0