[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-11-20
9
Medium Priority
?
413 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1180 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

831 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