in reply to Re^6: Position on Web Page
in thread Position on Web Page

Hi, Thank you for your reply. I have written code to find the location of the div:
 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
Basically it keeps adding all the left offsets for the parent nodes until i reach the top. Is this alright? Once i get that I can only approsimately get the right position. Say the node is 'p tag'. Then it could have lots of text which will eventually resize. So i can get my arrow somewhere in 'p tag' for sure. But what about the location in that. If you resize this browser window, you will see that the text above that i have written will readjust too. However the smallest dom node returned will be 'p tag' for the above text. I can get using dom the location of p tag but how can i maintin the position of the arrow within that 'p tag' tag so that it points to the same word? or will it always be approximate? Also, if I have a node which I have got using event object, can I store it in some form (like offset from root etc...) so that I can retrieve it at a later stage (e.g. when I revisit the page, I will use perl to place the arrow over there but for that i need to know the node which I had clicked earlier). Is there anyway to store the node information so that I can retrieve it later? Thank you for your time and effort. Anant

Replies are listed 'Best First'.
Re^8: Position on Web Page
by erroneousBollock (Curate) on Aug 20, 2007 at 16:11 UTC
    Something like the following will do the trick.

    The code is nasty hack, and is definitely not cross-browser compatible... it should, however, give you an idea of how to implement this in general.

    -David.

    Working (firefox only) example.

      Hi, Thankyou for the code you have written. But it may not necessarily be on text only. It could be anywhere on the page. I appreciate all the efforts you are all taking to solve my problem :) Thank you very much for all your reples. Anant
        I wrote it to look for text because that's what you said you were having trouble targeting.

        Normally, you can just compute the offsets of a node recursively to find it's position (most cases), but text nodes (nodeType == 3) are computed at page-reflow time and do not contain positioning attributes.

        All I did was to find the text you were looking for, split it apart from the surrounding text and then wrap it in a <span> tag... which has positioning attributes and so can be tracked as per any other node.

        No single method of searching/targeting the page content will work for everything, so you need to understand the types of content you will be serving and then adapt your "pointer" targeting correspondingly.

        Basically, my goal was to help you understand the nature of the problem, and a reasonable approach to its solution... but you will definitely need to work on it yourself (possibly do some reading about javascript and DOM) before anyone can help you more.

        I can help you with your version of the code if you like... but this thread is a little long and off-topic, so perhaps the CB and your public scratchpad might be better venues ?

        -David

Re^8: Position on Web Page
by erroneousBollock (Curate) on Aug 20, 2007 at 16:20 UTC
    Also, if I have a node which I have got using event object, can I store it in some form (like offset from root etc...) so that I can retrieve it at a later stage (e.g. when I revisit the page, I will use perl to place the arrow over there but for that i need to know the node which I had clicked earlier). Is there anyway to store the node information so that I can retrieve it later? Thank you for your time and effort.
    Do you mean you wish to record which piece of text (eg: a single word) was "pointed to" on one page view, then at a subsequent page view, restore the pointer to point to the previous position?

    If so, you might try sending the text to be matched to the server-side for storage (eg: in a database). When you next reload the page your server-side perl can generate a javascript statement to be executed on the client-side which tells the client-side "pointer" javascript which node to point to. An AJAX-style approach would probably work best.

    -David