1

Goal: Once the page is loaded a random area on the screen is clicked. Not a certain button or element, just a random click.

Maybe add an if function that if the click is successful then do another function?

  • Thank you for the constructive answer. As for the first part of your question, I do not know where to begin as an onLoad function would click the desired button element which is not the desired affect. – Realtime Dev Apr 15 '17 at 15:28
  • 1
    Possible duplicate of [How to simulate a click by using x,y coordinates in JavaScript?](http://stackoverflow.com/questions/3277369/how-to-simulate-a-click-by-using-x-y-coordinates-in-javascript) – Andreas Apr 15 '17 at 15:34

2 Answers2

0

using jquery, an approach can be...

var posx = (Math.random() * ($(document).width())).toFixed();
var posy = (Math.random() * ($(document).height())).toFixed();

$('body').append($("<div class=myclass style=left:"+posx+"px;top:"+posy+"px;>Hello from random!</div>"));


$( ".myclass" ).click(function() {
  alert( "random area clicked." );
});
.myclass{

position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
0

What about taking all divs, choosing a random one and click on it?

window.onload=function(){
   var els=document.getElementsByClassName("*");
   els[Math.round((els.length-1)*Math.random())].click();
};

And a click cannot be successful, and you cannot detect that...

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151