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

0

Follow the code:

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

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

JSFiddle: link

Anyone can change value using F12.

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

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

Is there a way before calling the function, check how many numbers are saved?

    
asked by anonymous 06.01.2017 / 18:35

2 answers

0

I think you want to do is stop the spread right, that's what I understood.

  

link

example:

<div onclick="(function(e) { e.preventDefault(); e.stopPropagation(); })(event)">

In your case, it would look like this:

<button onclick="(function(e) { e.preventDefault(); e.stopPropagation(); myFunction(1); })">Meu botão</button>

<script>
function myFunction(num) {
    alert(num);
}
</script>
    
06.01.2017 / 19:19
0

Hello, if your concern is that users will 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>
    
06.01.2017 / 19:31