0

I have an <a href="" class="auto_download" /> and would like to trigger, through Javascript a click (currently using jQuery; but some JS equivalent is ok too).

$('auto_download')[0].click();

Actually it works correctly if the <a href="" /> has a target="_blank" attribute; but the pop-up is blocked from the major browsers.

So, to avoid the issue, I removed the target=_blank" and it now does NOT work anymore. I guess this is related to some security policy of the browsers regarding what can be automatically clicked.

I've read different questions, I've seen many different solutions on SO, but couldn't find one that could be really cross-browser.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
  • You may try `$('.auto_download').get(0).click();` – Satpal Nov 03 '14 at 13:00
  • there's been a lot of issues around this subject, unfortunately there's no correct answer that matches all the cases – Amin Jafari Nov 03 '14 at 13:00
  • What exactly are you trying to do? An automatic refresh of the page? A form submitting? If you give us some additional details, maybe there's another (better) way to do this ;) – iMacTia Nov 03 '14 at 13:02
  • @iMacTia the `` links to a PDF file/attachment, that the user can download manually. But I've been asked also to automatically trigger the link after page loading, to let the user to download the file without having to search for the link. – Kamafeather Nov 03 '14 at 13:11
  • @Satpal Yes you are right. Fixed it.. – Kamafeather Nov 03 '14 at 13:13
  • Maybe this could help you to find a solution: http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery – Andreas Furster Nov 03 '14 at 13:18

2 Answers2

4

The code you have used,

$('.auto_download').click();

Would actually invoke the click handler attached to it. It wont make a physical click action over it. If you want to make a physical click then do,

$('.auto_download')[0].click();

But this wont be supported in touch device.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

Try pure js:

document.getElementByClassName("auto_download")[0].click();
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
DripDrop
  • 994
  • 1
  • 9
  • 18