0

can I create a function that takes x and y coordinates, and will click the document at those coordinates?

by looking at this question I set up some sample code in a jsfiddle, here is that code (it does not work)

var e = new jQuery.Event("click");
e.pageX = 10;
e.pageY = 10;
$("button").click(function(){
    $(document).trigger(e);
});

what I thought the above code would do is click in the document at position (10,10), and because that section of the document is where the item that needs to be clicked is, it should do the same as would clicking the item. But it does not...what am I doing wrong?

why

why would I do this when I could just simply do something like:

$("button").click(function(){
    $('.box').click();
});

well in certain circumstances, that is not possible. for example, clicking a specific div inside an iframe outside of the domain. I cannot click what is inside it through code because I don't have access to it's body. But if I know the position of that div in my own document and force the viewer to click over it, then that would work!

Community
  • 1
  • 1
Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
  • 2
    You can't do that. It's possible to locate an element by position (though tricky), but you still won't be able to trigger events in a cross-domain frame. – Pointy Jul 24 '13 at 18:30
  • you can't trigger events in the frame, but if there is, lets say, a link in the iframe. if the viewer clicks that link, it does go to the page. why can't I make it so that on `button` click, it clicks where that link is in the document if I know it's position...does that make sense? also this is not even working with a div right now – Ryan Saxe Jul 24 '13 at 18:34
  • if the code in the iframe is from the same domain, or generated from you you have access to the contents, using $("#myframe").get(0).contentDocument or contentWindow depending on the browser. – Patrick Evans Jul 24 '13 at 18:36
  • What you have does trigger an event on the document element, passing the event properties you created (http://jsfiddle.net/gK9D4/1/). But that does not mean that the browser will react as if the element on that position was clicked - it won't. – bfavaretto Jul 24 '13 at 18:37
  • @PatrickEvans I know that. This is just an example of why something like this may be useful (when you are using an iframe outside of the domain) – Ryan Saxe Jul 24 '13 at 18:37
  • @bfavaretto does that mean that I cannot force a click on an element based on position ever? – Ryan Saxe Jul 24 '13 at 18:39
  • I have never seen a logical reason to need to 1) show content outside my own domain, or 2) need to programatically click something in it except in the case of nefarious reasons. You should rethink your design. This is why non same domain iframes are sandboxed so that you cant run scripts like this. – Patrick Evans Jul 24 '13 at 18:39
  • @RyanSaxe It's a security issue. The browser won't let code from one domain make things happen to a DOM from another domain. – Pointy Jul 24 '13 at 18:42
  • I believe the only possible way is what Pointy mentioned: on every document click, check the click position against the position of all element, then trigger the event on the element itself when appropriate. – bfavaretto Jul 24 '13 at 18:43

2 Answers2

0

Well, you used

     $('button').click(function(){...});

Maybe, you meant,

 var e = new jQuery.Event("click");
 e.pageX = 10;
 e.pageY = 10;
 $(document).trigger(e);
thepanuto
  • 783
  • 5
  • 17
  • 1
    *"Well, you used...that's why it's failing."* It was perfectly clear that he was giving that as an example of something someone *might* suggest, and then went on to say why he didn't want that. No need to bring the sarky. – T.J. Crowder Jul 24 '13 at 18:34
  • @RyanSaxe: It's not you, it doesn't work. `document` will see the event, but not all the elements that the event would normally have bubbled through. http://jsbin.com/idibiz/1/edit – T.J. Crowder Jul 24 '13 at 18:40
0
var x=e.pageX;//get the current mouse coordinates
var y=e.pageY;

function between(val, min, max)//this fun specifies the range!!!
    {
        return val >= min && val <= max;
    }

Just a sample code.

 $('body').click(function(){    
if(between(x,your_conditional_min_x,your_conditional_max_x) && between(y,your_conditional_min_y,your_conditional_max_y))
//execute some code if its in my region!!!
});

In the above code-- your_conditional_min_x and y & your_conditional_max_x and y are the custom x and y positions that you want enclose your click in.

let me know if you are not clear with the code!!!

HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • I see that this will work, but it is not what I want...I want to be able to click a button, and when that is clicked, have it act as though a specific area of the document (that is not where the button was clicked) was clicked. – Ryan Saxe Jul 24 '13 at 18:52