Y
Ynot
I want to simulate a cursor moving across the Web page and pushing a button.
I wrote some JavaScript functions to move a graphic image. Works fine.
However, I hard-coded the x and y coordinates where the graphic image starts
(at a position below the button) and ends (at a position on the button) with
literal values, which is bad because not everyone uses the same screen
resolution I'm using.
I wrote more functions to calculate where to put the graphic image at the
beginning and where to move it to. Unfortunately, I'm getting an "error on
page" after I try to use document.getElementById( ) in my code. I've tried
several alternatives, including calling another function called findPosY( ).
I'm getting nowhere with that. It gives me "error on page," too.
Here is the HTML for the button. It's in the body, not inside a form or div
element.
<INPUT id=btn type=button value="OK">
Here's my code (located as a JavaScript script between <head> and </head>),
along with a few alternatives I've tried and commented out. I can assign
the document.getElementById('btn') to a variable successfully, but every
"num =" assignment I tried afterwards using that variable failed. Can
anyone please point out what I'm doing wrong and explain how to fix it?
Thanks.
function getStartPosTop()
{
//return 250; // works, but it's hard-coded.
var num;
var obj = document.getElementById('btn'); // can't I use this element
as an object?
num = obj.style.top + 150; // put the graphic 150 px below the btn
to push.
//num = obj.offsetTop + 150;
//num = obj.style.top;
//num = obj.offsetTop;
//num = obj.style.offsetTop;
//num = findPosY(obj);
return num;
}
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}
I wrote some JavaScript functions to move a graphic image. Works fine.
However, I hard-coded the x and y coordinates where the graphic image starts
(at a position below the button) and ends (at a position on the button) with
literal values, which is bad because not everyone uses the same screen
resolution I'm using.
I wrote more functions to calculate where to put the graphic image at the
beginning and where to move it to. Unfortunately, I'm getting an "error on
page" after I try to use document.getElementById( ) in my code. I've tried
several alternatives, including calling another function called findPosY( ).
I'm getting nowhere with that. It gives me "error on page," too.
Here is the HTML for the button. It's in the body, not inside a form or div
element.
<INPUT id=btn type=button value="OK">
Here's my code (located as a JavaScript script between <head> and </head>),
along with a few alternatives I've tried and commented out. I can assign
the document.getElementById('btn') to a variable successfully, but every
"num =" assignment I tried afterwards using that variable failed. Can
anyone please point out what I'm doing wrong and explain how to fix it?
Thanks.
function getStartPosTop()
{
//return 250; // works, but it's hard-coded.
var num;
var obj = document.getElementById('btn'); // can't I use this element
as an object?
num = obj.style.top + 150; // put the graphic 150 px below the btn
to push.
//num = obj.offsetTop + 150;
//num = obj.style.top;
//num = obj.offsetTop;
//num = obj.style.offsetTop;
//num = findPosY(obj);
return num;
}
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}