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

Not easily.

The most direct way to synchronize the position of the word with the pointer is to relatively position the pointer (wrt an invisible div surrounding the word)... but that would create text-flow problems around the target word.

The "walk up the tree" approach I mentioned is till the best way to reliably find the correct position irrespective of layout technique... also, showing the pointer as an absolutely positioned div (above the standard content) doesn't affect the flow of the document.

You can attach an event handler to the resize of the browser window such that the positioning code is re-called to reposition the pointer.

You should probably invest in some good books about unobtrusive Javascript and the HTML DOM.

-David

Replies are listed 'Best First'.
Re^7: Position on Web Page
by user2000 (Sexton) on Aug 20, 2007 at 07:10 UTC
    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
      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
      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