Trigger ~ Attribute Download HTML5

0

Good morning everyone, I need to download some text files (.txt) with the download attribute but I need to do this automatically when I load the page or a trigger that activates it, the problem is that I do not know how activate the element that has the attribute. I've tried

$('.meuElemento').trigger('click');

Thank you in advance.

    
asked by anonymous 11.11.2016 / 12:47

1 answer

1

You can do the following, between the a tags you can put the text inside a span with an identifier (in case I put the class download) and use the code to click one at a time, the problem is that the browser asks permission to download all files at once.

<a href="http://www.petcidade.com.br/wp-content/uploads/2016/09/cachorro-tenta-pegar-petisco-imagem-1-reproducao.jpg" download="teste"><span class="download">teste1</span></a>

<script>
    $('.download').trigger('click');
</script>

NOTE: The download attribute did not work for this link, but for a local file it worked on the test I did here!

Or you could do it this way here too:

$('a').each(function(i, el) {
    window.location = $(el).prop('href');
});

That way you would not need to put the span tag, but it would not have control by the download attribute.

    
11.11.2016 / 13:09