Solved

Getting The X and Y Absolute Position of a java applet using javascript?

Posted on 2003-11-20
9
390 Views
Last Modified: 2008-03-04
How can i get the x and y co-ordinates of a java applet in javascript???


I will be using the figure to position something in javascript. Which has to be abslutely positioned but i want to position it relative to the java applet.


thanks in advance
0
Comment
Question by:The_King
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9792018
try this, works with text boxes and such

var objTop=0;
var objLeft=0;

function getTop(obj){
      while (obj.offsetParent){
            objTop+=obj.offsetTop;
            obj=obj.offsetParent;
      }
}      

function getLeft(obj){
      while (obj.offsetParent){
            objLeft+=obj.offsetLeft;
            obj=obj.offsetParent;
      }
      
}

function getTopLeft(objGetPos){
      getLeft(objGetPos);
      getTop(objGetPos);
}      

called as getTopLeft(document.objName);
0
 
LVL 4

Expert Comment

by:apprenti
ID: 9792025
I haven't used applets recently. You could try using offsetLeft and offsetTop to get the co-ordinates of the top left hand corner of the applet, if that's what you mean.
0
 
LVL 4

Author Comment

by:The_King
ID: 9792051
I keep getting the error message offsetParent is not an object
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 25

Expert Comment

by:James Rodgers
ID: 9792100
i have only ever used it with input objects, radio buttons, text, checkboxes etc.

a work around is to make a hidden/display none check box in the same tables cell or div as the applet and call the function using the radio button.
0
 
LVL 4

Author Comment

by:The_King
ID: 9792108
I tried that with a text box that wasnt hidden and got the same error message
0
 
LVL 25

Accepted Solution

by:
James Rodgers earned 295 total points
ID: 9792309
i tried it with both a text area and a button and it gave the results, what browser and what version are you using?

0
 
LVL 15

Expert Comment

by:JakobA
ID: 9792310
beware of lines like
     objTop+=obj.offsetTop;
by the DOM specs  offsetTop  (and many other properties) are not just a number. they ar a string containing a number followed by a denominator  (ie: not  200  but '200px') Netscape (and Mozilla I guess) already obey this, so you should use parseInt to get the number:
     objTop+=  parseInt( obj.offsetTop, 10 );

regards JakobA

0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9792343
try this, slightly different

<html>
<head>
<script language="javascript">

function getRealLeft(el)
{
    xPos = eval(el).offsetLeft;
    tempEl = eval(el).offsetParent;
      while (tempEl != null) {
           xPos += tempEl.offsetLeft;
           tempEl = tempEl.offsetParent;
      }
    return parseInt(xPos, 10);
}

function getRealTop(el) {
    yPos = eval(el).offsetTop;
    tempEl = eval(el).offsetParent;
    while (tempEl != null) {
           yPos += tempEl.offsetTop;
           tempEl = tempEl.offsetParent;
      }
    return parseInt(yPos, 10);;
}

function test(){
   
      alert(getRealLeft(divTest) + ' ' + getRealTop(divTest));
}

</script>
</head>
<body onload="test()">
<div id="divTest" style="position:absolute; top:100; left:212;">
      here is the div
</div>
</body>
</html>
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9792349
thanks for the points!

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question