Difference between .click() and actually clicking a button? (javascript/jQuery)
button, click, javascript, jquery
Solution
I use this function to truly mimic a mouse click:
function clickLink(link) {
var cancelled = false;
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
cancelled = !link.fireEvent("onclick");
}
}
Problem
I'm trying to figure out this weird issue I've been having, and the root cause is the difference between a live click and triggering `.click()`. I won't get into the details of the problem, but basically when you click on the input button it works fine (has an `onclick` event). But if I call `.click()` from somewhere else (instead of physically clicking the button) it doesn't work properly. My question is - is there a way to truly replicate an actual click on a button? EDIT The problem: I'm opening a new window (aspx page) that loads an inline PDF. If I actually click on the link, the window opens fine and the PDF loads. If I use `.click()` the window opens and I'm prompted to download the PDF file. I've been through Adobe Reader settings, browser settings, and registry settings about the prompting - I understand that these can be factors to the big picture, but right now I'm concerned about why the behavior between a mouse click and .click() are doing anything different at all.