Results 1 to 18 of 18

Thread: Read position of a element??

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Read position of a element??

    I want to read the position of a element that is retrived with getElementById function of the document object..


    I have tried to read style.top and style.left with no success.
    Like this:

    abc = document.getElementById("test1");
    dfg = document.getElementById("test2");

    dfg.innerHTML = abc.style.top;


    The element is a div with a link in it..

    Help please!

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    Found this code in a other thread

    This works, but can someone explain it to me....

    if (document.getElementById('layer1').top) {
    var to = document.getElementById('layer1').top;
    }
    else if (document.getElementById('layer1').pixelTop) {
    var to = document.getElementById('layer1').pixelTop;
    }
    else if (document.getElementById('layer1').offsetTop) {
    var to = document.getElementById('layer1').offsetTop;
    }


    The last if case is executed, why?? When does the others execute??

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    Right position cant be read???

    Only left and top, or??

    Is there any way to read width then????


    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    whoa

    dude, you're talking to yourself there.

    The right position can't be read (as far as i know)
    for that, get the width of the layer, as specified within the <DIV> tag and add it to the left property.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    Anyone got any info on this: ?????????

    Found this code in a other thread

    This works, but can someone explain it to me....

    if (document.getElementById('layer1').top) {
    var to = document.getElementById('layer1').top;
    }
    else if (document.getElementById('layer1').pixelTop) {
    var to = document.getElementById('layer1').pixelTop;
    }
    else if (document.getElementById('layer1').offsetTop) {
    var to = document.getElementById('layer1').offsetTop;
    }


    The last if case is executed, why?? When does the others execute??

    How do I change this code to work with 4+ browsers?? This only works with IE 5.5+ and netscape 6, or??

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    Check out this images and tell me why this is....


    First, menus closed. Looks the same in both browsers..
    Attached Images Attached Images  
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    In MSIE 6.0...
    Attached Images Attached Images  
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    In Netsacpe 6.0...


    Seems that top postition of a table and td is not the same in IE and NS?!?

    /Smirre
    Attached Images Attached Images  
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  9. #9
    scoutt
    Guest
    in browser version 4 and below isn't it document.all ?instead of document.getElementbyID

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    does this work in both netscape and IE??

    how can I check wich type of browser it is???

    like:

    If (browser check something) {
    getElementByID("something") blah blah code
    }
    Else {
    document.all blah blah code
    }

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  11. #11
    scoutt
    Guest
    if browser
    Code:
    <SCRIPT>
     browser=((navigator.appName=="Microsoft Internet Explorer")&&(parseInt(navigator.appVersion)>=4));
     if (browser){ 
    getElementByID("something") blah blah code 
    } 
      Else
     { 
    document.all blah blah code 
    } 
      
    </SCRIPT>
    somehting like that, but don't quote me on it.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    That will only chosse between MSIE and Netscape.

    Not versions, that is what I have to chosse from.

    Can I just check it like this:

    if (document.getElementById("test")) {
    do some code
    }

    else {
    code for document.all
    }


    Will document.getElementById("test") return false if it doesnt is this type of document ?????

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  13. #13
    scoutt
    Guest
    that is what this does
    parseInt(navigator.appVersion)>=4
    detects the version of the browser. I don't go that far into javascript but only from what I have. sure that will check IE version as I don't know the exact code for Netscape.

  14. #14
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    This is the script I use for returned left/top/width/height

    function findleft(grandfather){
    if (document.all[grandfather].left){return document.all[grandfather].left;}
    if (document.all[grandfather].pixelLeft){return document.all[grandfather].pixelLeft;}
    if (document.all[grandfather].offsetLeft){return document.all[grandfather].offsetLeft;}
    return 'null';
    }

    function findtop(grandfather){
    if (document.all[grandfather].top){return document.all[grandfather].top;}
    if (document.all[grandfather].pixelTop){return document.all[grandfather].pixelTop;}
    if (document.all[grandfather].offsetTop){return document.all[grandfather].offsetTop;}
    return 'null';
    }

    function findwidth(grandfather){
    if (document.all[grandfather].offsetWidth){return document.all[grandfather].offsetWidth;}
    if (document.all[grandfather].clip.width){return document.all[grandfather].clip.width;}
    return 'null';
    }

    function findheight(grandfather){
    if (document.all[grandfather].offsetHeight){return document.all[grandfather].offsetHeight;}
    if (document.all[grandfather].clip.height){return document.all[grandfather].clip.height;}
    return 'null';
    }

    //find dom

    function findDom(objectid){
    if (document.getElementById){
    return document.getElementById(objectid);
    else if (document.all){
    return document.all(objectid);
    end if
    }

    btw. grandfather is relevent to my code

    Basically, you just call the functions where grandfather is the object id.

    The find dom function returns whether your browser is complient with getElementById (IE5/NS6) or the all DOM(IE4). You can use it like this...

    findDom('layer1').style.cursor='hand';

    ... for example, if you wanted to change the cursor to a hand.

    Any questions, feel free to ask.
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    Sorry scoutt, didnt see that to the right of the screen...

    punkpie_uk will document.all work in the older versions of Netscape too???


    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  16. #16
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    I dont think it'll work with netscape 4 because version 4 had its own DHTML method of using layers. eg

    document.layers('layer1')

    If you want to make it compatible with NS4 you'll have to write a script using the appName function and work out if the client uses it, then dynamically create the layers from a script which isnt really ideal.

    Or you could simply work out if they are using Netscape 4 and redirect them to the netscape updates site.

    I don't usually write code specific to Netscape 4 because only 15% of the online community use netscape and how many of them are using netscape 4?

    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  17. #17
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Try this : -

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Get Layer Positions</title>

    <script language="JavaScript" type="text/javascript">
    <!--
    function findleft(objectID){
    domStyle = findDom(objectID); // find style of dom
    if (domStyle.left){return domStyle.left;}
    if (domStyle.pixelLeft){return domStyle.pixelLeft;}
    if (domStyle.offsetLeft){return domStyle.offsetLeft;}
    return 'null';
    }

    function findtop(objectID){
    domStyle = findDom(objectID); // find style of dom
    if (domStyle.top){return domStyle.top;}
    if (domStyle.pixelTop){return domStyle.pixelTop;}
    if (domStyle.offsetTop){return domStyle.offsetTop;}
    return 'null';
    }

    function findwidth(objectID){
    if (domStyle.offsetWidth){return domStyle.offsetWidth;}
    if (domStyle.clip.width){return domStyle.clip.width;}
    return 'null';
    }

    function findheight(objectID){
    if (domStyle.offsetHeight){return domStyle.offsetHeight;}
    if (domStyle.clip.height){return domStyle.clip.height;}
    return 'null';
    }

    function findDom(objectid){
    if (document.getElementById){
    return document.getElementById(objectid);
    }else if (document.all){
    return document.all(objectid);
    }
    }

    //-->
    </script>

    </head>
    <body>

    <div id="layer1" style="position:absolute; left:150px; top:200px; width:200px; height:200px; background:cyan;">This is a layer</div>
    <input type="button" value="Get Left Position" onclick="alert(findleft('layer1'))">
    <input type="button" value="Get Top Position" onclick="alert(findtop('layer1'))">
    <input type="button" value="Get Width" onclick="alert(findwidth('layer1'))">
    <input type="button" value="Get Height" onclick="alert(findheight('layer1'))">
    </body>
    </html>

    Cut and paste it to see if it works correctly on your PC.

    Its pretty much the same as the code I wrote yesterday BUT it uses findDom to get whether the clients browser is complient with getElementById or all so it should work with all version 4 browsers up.

    FYI

    domStyle.left reads the left position of a element in NS4
    domStyle.pixelLeft reads the left posiiton in IE4+
    domStyle.offsetLeft reads the left position in NS6

    If you are still having problems reading the positions you might have to post the code you're working on and I'll have look at it, or send me an email. I'll be happy to help.
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    punkpie_uk, please check your mail and get back to me...

    The code is not made to work with other browsers than IE and Netcape latest versions... right now..

    /Smirre
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width