-6

I need to click on the specified xy position on a page without an actual click. I have tried many jquery and js scripts, but they did not work for me. Is this something possible with jQuery?

Edit: Not for any affiliate fraud ad clicks or something evil.

Ajmal Salim
  • 4,142
  • 2
  • 33
  • 41
  • 2
    I apologise if I'm wrong, but most of the reasons I can think of for wanting to do this are not good reasons. – Alex Hadley Oct 30 '11 at 10:54
  • @ahadley: Yeah, I smell kind of cheating – genesis Oct 30 '11 at 10:55
  • 5
    `@Ajmal:` In a comment on andreapier's answer, you've said *"I want my users to click on a link when they entered into that page. Its just only an ad.. "* FWIW, in all advertising systems I'm familiar with, that's fraud. Obviously I don't know what specific system you're using, but it's hard to imagine one where it's not. Separately, it's also very, very much not what I would want as a user. If I caught a site doing it, I would never go back to that site again. – T.J. Crowder Oct 30 '11 at 11:10
  • Your comments below show your use to be entirely inappropriate. – Alex Hadley Oct 30 '11 at 11:11
  • Rename to "How to do click fraud" maybe? ;) – Stein G. Strindhaug Oct 30 '11 at 11:13
  • Are there any StackExchange about fraud this could be migrated to perhaps? – Stein G. Strindhaug Oct 30 '11 at 11:19
  • Seriously: What's the policy about questions on how to do fraud and other criminal activity? Is this a close reason? – Stein G. Strindhaug Oct 30 '11 at 11:23
  • 1
    Why don't you just use a popup like every other annoying advertisement out there? – Reina Oct 31 '11 at 04:06

3 Answers3

3

Just put a div into your document with id click and trigger click with

$("#click").click();

I feel that clicking on specific cords is even impossible task

Example

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<div id="click"><a href="/advert/profit/1">advert here</a></div>
<script> $("#click").click(); </script>
genesis
  • 50,477
  • 20
  • 96
  • 125
  • Not working. Could you please explain this with an example? Also I need to click on a specific postion on screen. – Ajmal Salim Oct 30 '11 at 10:56
  • @Ajmal: added practical example – genesis Oct 30 '11 at 10:57
  • That will only fire handlers attached via jQuery, not handlers attached in any other way (DOM0, DOM2, IE's DOM2-like `attachEvent`, ...). – T.J. Crowder Oct 30 '11 at 10:59
  • @Ajmal: we won't do everything for you. You have to include jQuery framework – genesis Oct 30 '11 at 11:03
  • @Ajmal: So you'll be probably banned from the adservice, it'S 1000% against their TOS – genesis Oct 30 '11 at 11:03
  • @T.J.Crowder: Didn't even know this. So I have to use trigger()? – genesis Oct 30 '11 at 11:04
  • @Ajmal: Sure it will not work on adsense and Advert services which are good – genesis Oct 30 '11 at 11:06
  • @genesis: Actually, I think it might fire DOM0, at least on some browsers, but not DOM2: http://jsbin.com/ayedus I don't know that you can reliably simulate a DOM2 mouse click cross-browser. (I know you *shouldn't*.) – T.J. Crowder Oct 30 '11 at 11:07
  • 4
    @Ajmal: Who cares? It's still you faking an action to make money. Sounds like click fraud to me, even with an affiliate program. Certainly would be with Amazon. – T.J. Crowder Oct 30 '11 at 11:12
0

I think it's better if you bind the function you want to run to the load event itself... Emulating a mouse click generally is not a best programming practise.
Can you provide some code about what you are trying to do?

andreapier
  • 2,958
  • 2
  • 38
  • 50
  • I want my users to click on a link when they entered into that page. Its just only an ad.. – Ajmal Salim Oct 30 '11 at 11:02
  • 2
    Except this is a quite unfair operation, if you cannot specify your code we cannot help you. Keep attention to what you are doing: i'm pretty sure this is not admitted by you ad provider. – andreapier Oct 30 '11 at 11:08
  • If it's your own affiliate link you don't need to click it automatically... However @genesis answer should do the trick. Trust me, you'll get lots of problems doing what you want. Users will not trust you and will not come back to your site. DO NOT DO SUCH A THING. – andreapier Oct 30 '11 at 11:15
  • @Ajmal: Perhaps you should ask your ad provider if they have an API you can use to execute click fraud with them? ;P – Stein G. Strindhaug Oct 30 '11 at 11:28
0

Here is a complete test case that simulates the click event, calls all handlers attached (however they have been attached), maintains the "target" attribute ("srcElement" in IE), bubbles like a normal event would, and emulates IE's recursion-prevention. Tested in FF 2, Chrome 2.0, Opera 9.10 and of course IE (6):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function fakeClick(event, anchorObj) {
  if (anchorObj.click) {
    anchorObj.click()
  } else if(document.createEvent) {
    if(event.target !== anchorObj) {
      var evt = document.createEvent("MouseEvents"); 
      evt.initMouseEvent("click", true, true, window, 
          0, 0, 0, 0, 0, false, false, false, false, 0, null); 
      var allowDefault = anchorObj.dispatchEvent(evt);
      // you can check allowDefault for false to see if
      // any handler called evt.preventDefault().
      // Firefox will *not* redirect to anchorObj.href
      // for you. However every other browser will.
    }
  }
}
</script>
</head>
<body>

<div onclick="alert('Container clicked')">
  <a id="link" href="#" onclick="alert((event.target || event.srcElement).innerHTML)">Normal link</a>
</div>

<button type="button" onclick="fakeClick(event, document.getElementById('link'))">
  Fake Click on Normal Link
</button>

<br /><br />

<div onclick="alert('Container clicked')">
    <div onclick="fakeClick(event, this.getElementsByTagName('a')[0])"><a id="link2" href="#" onclick="alert('foo')">Embedded Link</a></div>
</div>

<button type="button" onclick="fakeClick(event, document.getElementById('link2'))">Fake Click on Embedded Link</button>

</body>
</html>

Demo here.

It avoids recursion in non-IE browsers by inspecting the event object that is initiating the simulated click, by inspecting the target attribute of the event (which remains unchanged during propagation).

Obviously IE does this internally holding a reference to its global event object. DOM level 2 defines no such global variable, so for that reason the simulator must pass in its local copy of event.

Conclusion - call javascript method as below in pageLoad or as you wish.

fakeClick(event, document.getElementById('link'))

Reference link

Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92