Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining all X,Y Co-ordinates of a page 2

Status
Not open for further replies.

ma701sd

IS-IT--Management
Dec 15, 2000
94
GB
Im trying to capture every X and Y Co-ordinate of a page.
Is there some kind of function of special tag that I culd learn so that when I run my mouse over any section of a page, an X and Y co-ordinate is given??

Any help would be greatly appreciated

Sam
 
function moveThing()
{
x = new Number(window.event.clientX);
y = new Number(window.event.clientY);
window.status = x + " , " + y
}

document.onmousemove = moveThing;

this works in ie adam@aauser.com
 
try this:

function mouseMoved()
{
var eobj,thex,they
eobj = window.event
thex = eobj.clientX
they = eobj.clientY
self.status = thex+","+they
}

document.onmousemove=mouseMoved

for IE...netscape, if I get a chance to figure it out, I'll post that too (probably not!) jared@aauser.com
 
Thanks...(Speedy response)

How would I include this in my HTML document?
 
just copy the code and paste it between <script> tags... either of these will work, but you may want to use mine because the variables are all local to the function... jared@aauser.com
 
If you use Netscape you have to use pageX(Y) instead of clientX(Y)

Comment: With IE clientX/Y gives you the coordinates as Screen Coordinates. if you would use offsetX/Y it would return the Coordinates relativ to the Object.

hnd
hasso55@yahoo.com

 
clientX/Y doesn't give you screen coordinates, it gives you window coordinates. adam@aauser.com
 
Thanks for the code,

Let me take this one step further...

What Im trying to do is:

Allow a user to place an image or text anywhere on the page.
I can do this via capturing an X and Y co-ordiante via a form, passing the variable to a action page, and then using CSS and Coldfusion to use the variables to place the text/image according to the co-ordinate. This is rimitive but it works :)
However, the user needs to be able to visually see where they want to place the image or text on the input page, therefore I was thinking a series of checkboxes where a user can choose two checkboxes (to represent an X and Y Co-ordinate). This is then captured and displayed on the actionpage accordingly.
I dont think ive explained it very well, If a more detailed explanantion is required, then please let me know.

i am attaching my code to help clarify what I'm trying to do:

Createpage.cfm (this is the page where the users X'Y's co-ordinate is captured, but at the moment, the user is entering the co-ordinates blindly i.e. unsure what the co-ordinate (100,100) will yield)

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>
</head>

<body>

<FORM ACTION= &quot;previewXYpage.cfm&quot; METHOD=&quot;POST&quot;>

Enter your X Co-ordinate <INPUT TYPE =&quot;Text&quot; NAME=&quot;X&quot;><BR>
Enter your Y Co-ordinate <INPUT TYPE =&quot;Text&quot; NAME=&quot;Y&quot;><BR>


<INPUT TYPE = &quot;Submit&quot; VALUE =&quot;Enter&quot;>
<INPUT TYPE = &quot;Reset&quot; VALUE =&quot;Clear&quot;>

</FORM>
</body>
</html>

This is the actionpage where the variables are retrieved and positioned accordingly:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>

<STYLE TYPE=&quot;text/css&quot;>
#companyname
{
position:absolute;
top: <CFOUTPUT>#X#</cfoutput>px;
left: <cfoutput>#Y#</cfoutput>px;
}
</style>

</head>

<body>
<DIV ID=&quot;CompanyName&quot;>
YYY

</div>
</body>
</html>


I hope this visually clarifies what Im trying to do :)

ANyhelp would be greatly appreciated

Many thanks
Sam
 
<style>
.clickablearea{cursor:hand;}
</style>
</head>

<body>
<script>
function mouseMoved()
{
var eobj,thex,they
eobj = window.event
if(eobj.srcElement.className==&quot;clickablearea&quot;)
{
thex = eobj.clientX
they = eobj.clientY
document.formname.x.value=thex
document.formname.y.value=they
}

}

document.onclick=mouseMoved
</script>

<div class=&quot;clickablearea&quot;>Ma701sd, you can store these coords.</div><br><br>
<div>But not these...</div>
<form name=&quot;formname&quot;>
<input name=x><input name=y>
</form> jared@aauser.com
 
Hi,

I was trawling through my emails and I found the reply for this post I put up ages ago!

Thanks for your replys :)

I tried it and it works although it's not exactly how I want it to work however you have provided the mechanics of what I need.

Can ask a favour, Could you possibly provide some coments (in real layman terms) as i've never done javascript before?

Also I modifed what you did to make it fit with what I'm doing, but I get a runtime error and asks would I like to debug? WHy does it do this, heres the code:
<style>
.clickablearea{cursor:hand;}
</style>
</head>

<body>
<script>
function mouseMoved()
{
var eobj,thex,they
eobj = window.event
if(eobj.srcElement.className==&quot;clickablearea&quot;)
{
thex = eobj.clientX
they = eobj.clientY
document.formname.x.value=thex
document.formname.y.value=they
}

}

document.onclick=mouseMoved
</script>

<div class=&quot;clickablearea&quot;>Ma701sd, you can store these coords.</div><br><br>
<div>But not these...</div>
<!--- <form name=&quot;formname&quot;> --->
<!--- <input name=x><input name=y> --->

<FORM ACTION= &quot;previewXYpage.cfm&quot; METHOD=&quot;POST&quot;>

Enter your X Co-ordinate <INPUT NAME=X><BR>
Enter your Y Co-ordinate <INPUT NAME=Y><BR>


<INPUT TYPE = &quot;Submit&quot; VALUE =&quot;Enter&quot;>
<INPUT TYPE = &quot;Reset&quot; VALUE =&quot;Clear&quot;>

</FORM>
</body>
</html>

Once Ive obtained the co-ordinate, I want to pass it to the next page for which I have have the code to manipulate..Is this correct in terms of the logisitics:

<STYLE TYPE=&quot;text/css&quot;>
#companyname
{
position:absolute;
top: <CFOUTPUT>#X#</cfoutput>px;
left: <cfoutput>#Y#</cfoutput>px;
}
</style>

</head>

<body>

<DIV ID=&quot;CompanyName&quot;>
the text variable positioned according the X and Y co-ordinates from the previous page

Many thanks and best regards

Samir
 
sorry, I forgot to mention...

The error I get when I click on &quot;Ma701sd, you can store these coords&quot; is &quot;document.formnanme.x is null or not an object&quot;

Also, what do you mean when you said that the X/Y co-ordinates are given only for the screen co-ordinates/windows co-ordinates?

Once again, I really appreciate your help :)

Best Regards

Sam...
 
where are you getting the error? I'm not sure about coldfusion...

you need to change the name of the form and form elements in the script to fit your exact html names jared@aauser.com
 
Hey Jaredn,
Thanks, I got it sussed thanx,

But could you please provide some comments to the code as I dont fully understand what it's doing.

Also, what did you mean when you said
&quot;X/Y co-ordinates are given only for the screen co-ordinates/windows co-ordinates?&quot;

Many thanxs for your help :)
 
function mouseMoved()
{
var eobj,thex,they
eobj = window.event
//store the event object
if(eobj.srcElement.className==&quot;clickablearea&quot;)//check to see if its &quot;clickable&quot;
{
thex = eobj.clientX //store y coord
they = eobj.clientY //store x coord
//display them
document.formname.x.value=thex
document.formname.y.value=they
}

}

document.onclick=mouseMoved
//every time the mouse moves call mouseMoved jared@aauser.com
 
Hey jaredn,

Thanx for your help.
Sorry to keep this going...
But..
Is it possible to extend the range of the &quot;clickable area&quot; e.g. make half the page or the whole page clickable?

Many thanx and best regards

Sam
 
Hi Jaredn,
Sorry, but I have another question that I hope you might be able to help me with..
WHat I have done is embedded the code within two forms (Eventually I want to make as many forms as there are elements that I want to position). WHat I have done is made the clickable area a table row, but, As I have two forms and I click on the top row, the co-ordinates are displayed in the second forms input box. I hope this makes sense...I have posted the code..Please help :)

<form name=&quot;previewXYpage.cfm&quot; ACTION =&quot;previewXYpage.cfm&quot; METHOD=&quot;Post&quot;>
<table bgcolor=&quot;#COCOCO&quot; cellspacing=&quot;1&quot; cellpadding=&quot;1&quot; border=&quot;2&quot;>
<tr>
<td WIDTH=1%><div class=&quot;clickablearea&quot;>This a clickable row</td></div>
</tr>
</table>

The X Co-Ordinate is <input type=&quot;text&quot; name=&quot;x&quot; Value=0><BR>
The Y Co-Ordinate is <input type=&quot;text&quot; name=&quot;y&quot; Value=0>

<INPUT TYPE = &quot;Submit&quot; VALUE =&quot;Enter&quot;>
<INPUT TYPE = &quot;Reset&quot; VALUE =&quot;Clear&quot;>

</FORM>

<form name=&quot;formname&quot; ACTION =&quot;previewXYpage.cfm&quot; METHOD=&quot;Post&quot; >
<table bgcolor=&quot;#COCOCO&quot; cellspacing=&quot;1&quot; cellpadding=&quot;1&quot; border=&quot;2&quot;>
<tr>
<td WIDTH=1%><div class=&quot;clickablearea&quot;>This a clickable row</td></div>
</tr>
</table>

The X Co-Ordinate is <input type=&quot;text&quot; name=x Value=0><BR>
The Y Co-Ordinate is <input type=&quot;text&quot; name=y Value=0>

<INPUT TYPE = &quot;Submit&quot; VALUE =&quot;Enter&quot;>
<INPUT TYPE = &quot;Reset&quot; VALUE =&quot;Clear&quot;>

</FORM>

</body>
</html>
 
Hi Jaredn,

is there anyway that I can extend the clickable area?

your help will be greatly appreciated

thanx

sam....

 
you can either add the class=&quot;clickable area&quot; to any element on the page, or just remove that code entirely:

if(eobj.srcElement.className==&quot;clickablearea&quot;)

take out above and accompanying braces that enclose the if code

than any part of the page will be 'clickable'...
jared@aauser.com
 
Thanx Jaredn,

I tried the second option i.e remove the
if(eobj.srcElement.className==&quot;clickablearea&quot;)
but what happens now is that when I submit to the action page by clicking &quot;enter&quot; button, the co-ordinates of the &quot;enter&quot; button are captured every time...
Is there anyway to stop this from happening?

Thanx

Sam
 
change it to say:

if(eobj.srcElement.className!=&quot;clickablearea&quot;)

and put the clickablearea class inside the submit button. The problem right now is that when you click on the button it is stroing those coordinates.

jared@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top