/    Sign up×
Community /Pin to ProfileBookmark

Control coordinates on screen

Hi to all,

my problem was to detect the coordinates for each control on the web page. Since I was not successful in finding any property to retrieve
such information, I wrote the following code to get it out.
The code follows at the bottom of this post.
Basically, one calls getOBJfunction( id, “top” ) and getOBJfunction( id, “left” ) to retrieve the Y and X coordinate of the control within the
webpage. The parameter id indicates the id property of the given control,
according to the nomenclature used along the web page.
Thus, for example, the position of <INPUT ID=”item1″> can be
tracked down via

  • * getOBJposition( “item1”, “top” );

  • * getOBJposition( “item1”, “left” );
  • My personal use is to place floating divs over the screen and in particular below a input control where data search are performed.

    I wanted to post this message to have suggestions for further improvements.

    Thanks in advance to all checking in this topic.

    Alessandro Rosa

    [code]
    function getOBJposition( id, dim )
    {
    var obj = document.getElementById( id );
    var top = 0 ; var left = 0 ;

    if ( obj != null || dim.length > 0 )
    {
    dim = dim.toLowerCase();

    while( obj.tagName.length > 0 )
    {
    if ( obj.tagName.toUpperCase() == “BODY” ) break ;

    if ( dim == “top” && obj.tagName != “TR” && obj.tagName != “TBODY” )
    {
    top += obj.offsetTop ;
    top -= obj.clientTop ;
    }
    else if ( dim == “left” && obj.tagName != “TBODY” )
    {
    left += obj.offsetLeft ;
    left -= obj.clientLeft ;
    }

    obj = obj.parentNode ;
    }

    if ( dim == “top” ) return top ;
    else if ( dim == “left” ) return left ;
    }
    else
    {
    top = left = width = height = -1 ;
    return false ;
    }
    }
    [/code]

    to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @zandor spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...