There is an array of random phrases when you click on a button, it gives a random array from the array. Question: How to create a txt file using JS or HTML5 or jQuery when you click on a link with a phrase, I didn’t even find anything close on the Internet. Help plz at least with the literature on this topic, the sample code would be absolutely magical!

Here is the array code >>

function my_alert() { var text = ["Сегодня прекрасный день", "Вам определённо повезёт", "Не грусти. Всё будет хорошо", "Улыбнись", ":) :) :)", "Ты сегодня лучше всех"]; min = 0; max = text.length-1; out = Math.floor(Math.random()*(max-min+1))+min; document.getElementById("text").innerHTML = text[out]; } 
 <p>Случайная фраза:</p> <a href="#"><p id="text"></p></a> <button id="ok" onClick="my_alert()">ОК</button> 

2 answers 2

For such purposes there are special plugins. I used this: https://github.com/eligrey/FileSaver.js

Or you can also:

 var downloadURL = function(url, name) { var link = document.createElement('a'); if(name == undefined || name == ''){name = url}; link.setAttribute('href',url); link.setAttribute('download',name); onload = link.click(); }; $(function() { $('input[type="button"]').click(function() { downloadURL('data:text/plain;charset=UTF-8,'+$('#text').val(), 'Название'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="text"></textarea> <input type="button" value="Скачать"> 

  • Thank you, and if you already used it, could not throw an example ?. if it doesn't bother you - Evgeny Lyubimov
  • @ YevgenyLyubimov, that was a long time ago. I did a web code editor and I needed to save the code in a file - Yuri
  • I asked if it doesn’t make it difficult =) but thanks anyway - Evgeni Lyubimov
  • mega.nz/#!zlURBaZJ!Xd09PF9E4nnqj67e99fbEcSH0F4vdum-Onw9NTaNI-4 So I did, but I didn't do everything in the editor there :) - Yuri
  • Required plugin? Text file? In my opinion it is possible in one line for the data-protocol. - nick_n_a

No Access to the file system from the browser is denied

  • one
    Not quite right, access to anything - yes, is denied. - Duck Learns to Take Cover
  • @Anton Shchyrov volumetric answer =), and if a file is created in advance to place in it, a phrase and download the file via the link? - Evgeny Lyubimov
  • @YevgenyLyubimov you can not save in any folder, just create a file in a folder in advance provided by the browser - you can. See my comment on the question. - Duck Learns to Take Cover
  • @ YevgenyLyubimov well, yes, the option "create a file on a server and click on the link only download" is also possible. As I understand it, the task is purely for self-study or is there a practical goal? - Duck Learns to Take Cover
  • one
    @Anton Shchyrov, in general, in the context of the problem, your answer “No way” is false, I recommend deleting or correcting - Duck Learns to Take Cover