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.

6
  • You may try $('.auto_download').get(0).click(); Commented Nov 3, 2014 at 13:00
  • there's been a lot of issues around this subject, unfortunately there's no correct answer that matches all the cases Commented Nov 3, 2014 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 ;) Commented Nov 3, 2014 at 13:02
  • @iMacTia the <a href="" /> 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. Commented Nov 3, 2014 at 13:11
  • @Satpal Yes you are right. Fixed it.. Commented Nov 3, 2014 at 13:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Try pure js:

document.getElementByClassName("auto_download")[0].click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.