Jul 16th

JavaScript – why simulating a click using the click() method doesn’t work in Chrome

2011, 22:56 UTC | By | In Development
Leave a comment | Trackback  | 1359 views

Chrome doesn't allow simulating a click using the click() method.

While working on some WordPress plugin to enhance the comment system with a couple of Ajax features, i ran into a problem. My code was using the click() method on a link object to simulate a click on this link and while this did work fine in Opera, Firefox and Internet Explorer, it did not so in Google Chrome. Now, I know using click() is a bad thing and there is almost always a better way but in this specific case, there wasn't so I had to find a solution.

The reason why Chrome doesn't allow this is most likely related to its security model and I think it does it on purpose which is all fine, because  such functionality is rarely, if ever, needed.

 So here is a solution that seems to work:

var cancel_button = document.getElementById('cancel-comment-reply-link');
if(isValidObject(cancel_button)) {
    if(cancel_button.style.display != 'none') {
        try {
            cancel_button.click();
        }
        catch(e) {
            var event = document.createEvent("MouseEvents");
            event.initMouseEvent("click", true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
            var _r = !cancel_button.dispatchEvent(event);
        }
    }
}

The code assumes that cancel_button is a simple link and isValidObject() is just a helper function in my code to check whether an object does exist to avoid JavaScript errors. The try/catch construct is another necessity, because some testing revealed that the event method does not work in Internet Explorer, but the simpler click() method works just fine. Oh well, browsers...

I know that  cancel-comment-reply-link and its response to click events is handled by some WordPress JavaScript code in comment-reply.js, so a different solution would be to use just this code, but I opted against this method, because it would probably break when this code would change in a future WordPress update.

2 Likes Like
 

2 responses to: JavaScript – why simulating a click using the click() method doesn’t work in Chrome

  1. Bjasd, Jul 18th, 2011 at 10:31
    Reply | Quote | #1

    Didn't Firefox not prevent it too until recently? I remember reading something about this in a bug report linked from the commit logs.

    • Admin, Jul 18th, 2011 at 10:46
      Reply | Quote | #2

      Bjasd, :

      Didn't Firefox not prevent it too until recently? I remember reading something about this in a bug report linked from the commit logs.

      I think so, yes. That's why I'm using the try/catch method - if the cancel_button.click() fails, the other method will take over and it's required anyway, because the event method doesn't work in IE8. So far, this still works on recent Firefox nightly builds (8.0a1).

      Normally, I would strongly opt against using simulated clicks anyway, because it is downright ugly, but in this very special case, there was no other way to do it.

Subject

  (this is optional)

Comment text

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>