0

hello im trying to get a script to run so that a link auto clicks after certain amount of time

on familyoffices.com the link is the "we are online" graphic at the bottom right

im using

  <script type="text/javascript">
    $(document).ready(function() {
      setTimeout(function() {
    $('#desginstudio-button-image-desktop').click();
    }, 300);
 });
   </script>

unfortunately this isn't firing off....anyone know how i can achieve this?

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
Spaceliving
  • 31
  • 1
  • 15
  • Is there really such an element? Is it definitely `desginstudio`, not `designstudio`? –  May 11 '18 at 21:03
  • i fixed the id and used this – Spaceliving May 11 '18 at 21:16
  • still no go with the above code – Spaceliving May 11 '18 at 21:16
  • Are there any errors in the console? –  May 11 '18 at 21:20
  • nothing that stands out to me – Spaceliving May 11 '18 at 21:23
  • i just realized the element i want clicked in inside a loaded iframe... how do i drill down into that? is it possible? – Spaceliving May 11 '18 at 21:25
  • As you mention in a comment on @Shaun Forsyth answer, the iframe may be your issue. I commented on a [related question](https://stackoverflow.com/questions/41647042/invoke-my-jquery-function-on-a-webpage-opened-in-iframe/50166823#50166823) the other day. You can't access things not on your server. Security. – PoorlyWrittenCode May 12 '18 at 01:43

5 Answers5

2

Try using

<head>
<script>
function haveclicked(){
document.getElementById('myLink').click();
}
</head>
<body onload="setTimeout('haveclicked();',3000);">
<a id="myLink" href="http://www.google.com" target="_blank">GOOGLE</a>
</body>
Karan Tewari
  • 498
  • 8
  • 20
  • As @stephanie mentioned above, yes the click event is mostly for IE and its not recognized by all browsers so yea you can also use the above suggestion or create a new function with addEventListener where you can define what happens on click event. – Karan Tewari May 11 '18 at 21:11
  • i tried this and it doesn't work :/ you can see it on the inspector but nothing – Spaceliving May 11 '18 at 21:17
  • Well, this seems to be strange, seems like jsfiddle is blocking my trigger, checked the same with codepen and it worked https://codepen.io/karan_tewari601/pen/qYYKrx – Karan Tewari May 11 '18 at 21:25
1

Consider this from the MDN.. You need to create an event to do this correctly.

function simulateClick() {
  var evt = new MouseEvent("click", {
    bubbles: true,
    cancelable: true,
    view: window
  });
  var cb = document.getElementById("checkbox"); //element to click on
  var canceled = !cb.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
}
document.getElementById("button").addEventListener('click', simulateClick);
<p><label><input type="checkbox" id="checkbox"> Checked</label>
<p><button id="button">Click me</button>
Shaun Forsyth
  • 454
  • 4
  • 7
  • the element i need is loaded inside a iframe – Spaceliving May 11 '18 at 21:29
  • if you can target the element in a frame, so CORS is not in play (click jacking), then this should still work. – Shaun Forsyth May 11 '18 at 22:07
  • problem is i cannot edit the target code since its coming from a iframe controlled from a third party. '
    '
    – Spaceliving May 14 '18 at 06:40
  • above is the code thats gets placed on the website from the third party plugin – Spaceliving May 14 '18 at 06:41
  • Then you most likely wont be able to access it, and this is a good thing for the internet, but not you. More information from OWASP https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet, basically, this stop you from stealing my bank details. – Shaun Forsyth May 14 '18 at 15:52
1

Instead of

$('#desginstudio-button-image-desktop').click();

Try using

$('#desginstudio-button-image-desktop').trigger('click');
swathi_sri
  • 417
  • 4
  • 8
1

You can try to first locate the iframe and the element in it --

var frame = document.getElementById(/* your frame id*/),
    button = frame.contentDocument.getElementById(/* your button id*/);

Then dispatch a synthetic event --

button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
  • im not sure how to target this being that its not a button element but a div.
    – Spaceliving May 14 '18 at 06:38
  • Locate the iframe, then, using the iframe's `contentDocument`, locate whatever expects a click inside it and dispatch a click event to that element. –  May 14 '18 at 07:39
0

Some browsers/versions don't support using click() to trigger a click event. Try using .trigger('click') instead