How do I click a link with javascript?


/ Published in: JavaScript
Save to your folder(s)

Let you fire click events on any html element.


Copy this code and paste it in your HTML
  1. HTMLElement.prototype.click = function() {
  2. if (document.createEvent) {
  3. var evt = this.ownerDocument.createEvent('MouseEvents');
  4. evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
  5. this.dispatchEvent(evt);
  6. } else if (this.fireEvent) {
  7. this.fireEvent("onclick");
  8. }
  9. }
  10.  
  11. // usage
  12. // <a href="http://www.barattalo.it" id="linktoclick">auto click</a>
  13. document.getElementById("linktoclick").click();

URL: http://www.barattalo.it/2009/11/18/click-links-with-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.