Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-20
9
Medium Priority
?
401 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

609 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