Onclick calls function 2 times after changing value in browser developer mode

Asked

Viewed 78 times

0

Follows the code:

<button onclick="myFunction(1)">Meu botão</button>

<script>
function myFunction(num) {
    alert(num);
}
</script>

Jsfiddle: https://jsfiddle.net/43mt9og8/

Anyone can change value using F12.

inserir a descrição da imagem aqui

Change number myFunction(1) for myFunction(2), and then click button, call the javascript function 2 times.

Each time you change the number, you record it somewhere. (If it changes 50 times, it will call the function 50 times)

There is way before calling the function, check how many numbers recorded ?

  • 1

    Client-side code (in this case javascript) is always at the mercy of "curious" customers, you can make it difficult, but you can’t help it. You can make it difficult for example by taking the "onclick" attribute from html, so it’s not as visible. Ex de brincar com o codigo do lado cliente no google: https://postimg.org/image/6xv0q6lml/

  • @Miguel, how did the guy make this site ? http://www.galaxyrom.com/ nor f12 or botão mouse direito doesn’t work.

2 answers

0

0

Hello, if your concern is that users do not change your javascript code, you will need a code like the one below. And if possible separate the javascript file from the html file.

<button id="btnId">Meu botão</button>
<script>
    function myFunction(num) {
        alert(num);
    }
    document.getElementById('btnId').onclick = function(){
        myFunction(47);
    };
</script>
  • Hi @Bruno Gerotto, in this case it is not possible, because each button has its random value.

  • How these random button values are generated?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.