www.webdeveloper.com
Results 1 to 11 of 11

Thread: Javascript click method

  1. #1
    Join Date
    May 2010
    Posts
    5

    Question 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,

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Exclamation

    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.

  3. #3
    Join Date
    Dec 2009
    Posts
    24
    look into the click method it kind of looks like

    thisButton.click();

  4. #4
    Join Date
    May 2010
    Posts
    5
    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?

  5. #5
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,478
    Hi there majmal,

    have a look at this example, you may be able to adapt the methodology to suit your requirements.
    Code:
    
    <!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>
    
    Click anywhere to see the result.

    coothead

  6. #6
    Join Date
    May 2010
    Posts
    5
    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.

  7. #7
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,478
    Quote Originally Posted by majmal View Post

    "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".
    Your first post, though, had this line....
    Quote Originally Posted by majma

    "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
    ".
    It appears that you have now decided to move the goalposts.

    coothead

  8. #8
    Join Date
    May 2010
    Posts
    5
    Sorry not that great with words lol

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    If not onclick, maybe onmousemove? If so:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    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
    </script>
    </head>
    <body>
    <span id="myspan">&nbsp;</span>
    </body>
    </html>

  10. #10
    Join Date
    May 2010
    Posts
    5
    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>
    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).

  11. #11
    Join Date
    Jul 2007
    Posts
    386
    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 FIND which is the first element UNDER 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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles