Solved

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

Posted on 2003-11-20
9
382 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now