/    Sign up×
Community /Pin to ProfileBookmark

Javascript click method

Hey guys,

I am not that great with javascript but I’ve been trying to create a little script that automatically clicks a certain position on the window.

I am not 100% sure how to do this. Can someone please help.

What I want it to be able to do is find out where the position of the mouse currently is (this part I already know how to do) and then click those coordinates)

Thanks,

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 27.2010 — I'm not 100% sure you can do that.

It would be like taking over someone's computer and pushing/clicking buttons

that the user might not want to do or wish to be re-directed to. :eek:
Copy linkTweet thisAlerts:
@skullsnestMay 27.2010 — look into the click method it kind of looks like

thisButton.click();
Copy linkTweet thisAlerts:
@majmalauthorMay 27.2010 — I know you can use javascript click a link by doing

document.getElementById('linkID').click();

so maybe there is a way to use this to click a perticular coordinate?
Copy linkTweet thisAlerts:
@cootheadMay 27.2010 — Hi there majmal,

have a look at this example, you may be able to adapt the methodology to suit your requirements.
[color=navy]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
body {
margin:0;
height:100%;
}
#box {
position:absolute;
z-index:999;
width:100%;
height:100%;
}
#box a {
display:block;
width:100%;
height:100%;
cursor:default;
outline:none;
}
</style>

<script type="text/javascript">

function init(){
if(window.addEventListener){
document.body.addEventListener('mousemove',curPos,false)
}
else {
if(window.attachEvent){
document.body.attachEvent('onmousemove',curPos);
}
}
}

function curPos(event){

obj=document.getElementById('box').style;

l=event.clientX
t=event.clientY;

obj.top=t-screen.width/2+'px';
obj.left=l-screen.height/2+'px';

}
if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}

</script>

</head>
<body>

<div id="box">
<a href="http://www.webdeveloper.com/forum/showthread.php?t=230342"></a>
</div>

</body>
</html>
[/color]


Click anywhere to see the result. ?

[i]coothead[/i]
Copy linkTweet thisAlerts:
@majmalauthorMay 27.2010 — Unfortunately, thats not what I need to do. I need this function to be triggered automatically not by the user having to click on the page.
Copy linkTweet thisAlerts:
@cootheadMay 27.2010 — [color=navy]

"Unfortunately, thats not what I need to do. I need this function to be triggered automatically

[size=3][b] [color=red]not by the user having to click[/b][/size][/color] on the page". [/color]

[/QUOTE]

Your first post, though, had this line....
[color=navy]

"What I want it to be able to do is find out where the position of the mouse currently is

(this part I already know how to do) and then [size=3][b][color=red]click those coordinates[/color][/b][/size][/color]
".[/QUOTE]


It appears that you have now decided to move the goalposts. ?

[i]coothead[/i]
Copy linkTweet thisAlerts:
@majmalauthorMay 28.2010 — Sorry not that great with words lol
Copy linkTweet thisAlerts:
@KorMay 28.2010 — If not onclick, maybe onmousemove? If so:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
var posx;var posy;
function getMouse(e){
posx=0;posy=0;
var e=(!e)?window.event:e;//IE:Moz
if (e.pageX){//Moz
posx=e.pageX+window.pageXOffset;
posy=e.pageY+window.pageYOffset;
}
else if(e.clientX){//IE
if(document.documentElement){//IE 6+ strict mode
posx=e.clientX+document.documentElement.scrollLeft;
posy=e.clientY+document.documentElement.scrollTop;
}
else if(document.body){//Other IE
posx=e.clientX+document.body.scrollLeft;
posy=e.clientY+document.body.scrollTop;
}
}
else{return false}//old browsers
document.getElementById('myspan').firstChild.data='X='+posx+' Y='+posy;
}
document.onmousemove=getMouse
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;span id="myspan"&gt;&amp;nbsp;&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@majmalauthorJun 02.2010 — umm maybe i am just not explaining it right.

The idea is to use the following script in a new way
[CODE]
<script language="Javascript" type="text/javascript">
document.getElementById('mylink').click()
</script>
[/CODE]


I want to be able to click on a certain part of the screen (window) without the user having to do anything.

I was hoping there is a way to replace getElementById with something that clicks any element in a certain position in the window (x,y coordinates).
Copy linkTweet thisAlerts:
@SparoHawkJun 02.2010 — It simply is not possible to force click in the way you want. All you can do is simulate.

First of all, you would need to pick the coordinate where the click will be. Second, you would need to [B]FIND[/B] which is the first element [B]UNDER[/B] those coordinates so that then you can invoke that elements click() function.

That is one hell of a job if you ask me. You'd have to do tremendous calculations and storage of elements positions relative to the page's X and Y, plus the elements dimensions AND its z-index.

Impossible, no, but darn near close. Good luck.
×

Success!

Help @majmal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...