A
A
Alex Lav2020-05-09 01:49:27
PHP
Alex Lav, 2020-05-09 01:49:27

How to execute a script on button click?

Hello, help me load the MC.ini file with the username={login} information recorded when clicking on the link with id=""
Here I have compiled the code, but for some reason it only works after the second click.
Help me find the error or tell me how to download this file.
Thanks in advance!

<script>
 function copytext(el) { 
  var $tmp = $("<input>"); 
  $("body").append($tmp); 
  $tmp.val($(el).text()).select(); 
  document.execCommand("copy"); 
    swal("IP адрес скопирован!", " ", {
  buttons: {
    cancel: "Ок",
    catch: {
      text: "Запустить игру",
      value: "catch",
    },
  },
})
.then((value) => {
  switch (value) {
 
    case "catch":
  swal("Программа сейчас запустится!", "Подождите немного. Приятного отдыха!", "success");
  
/* Кликает на ссылку #play788mc */
          document.querySelector('.playrustex').click()

/* Скачивает файл MC.ini */
    document.getElementById('todownload').onclick = function() {
    let text = "username={username}";
    let myData = 'data:application/txt;charset=utf-8,' + encodeURIComponent(text);
    this.href = myData;
    this.download = 'MC.ini';
}
      break;
 
    
  }
});
  $tmp.remove(); 
}
   
    </script>

<a id="todownload" href="#play788mc" type="button" class="playrustex"></a>


If the link contains the word "play788mc", my program launches another program that is located next to it in the folder.
It is necessary that the link contains the word play788mc and the file is also downloaded, following the link is not necessary, it is enough to have the right word, so I made a reference to the non-existent attribute #play788mc

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew Kolomiets, 2020-05-09
@Coolam

<a id="todownload" href="#play788mc" type="button" class="playrustex" onClick="copytext(el)"></a>

This is so that the script is executed when the element containing the onClick is clicked.

A
AUser0, 2020-05-10
@AUser0

Move the link click to .click() so that it fires AFTER assigning the download function to the click.
And yet, it's better to click not on the class (too general), but on the id, which, of course, must be unique.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question