Javascript disable function after 1 click

  • A+
Category:Languages

I read more than 10 articles, I tried many codes but I did not find a solution, so this is my code I want to disable this function after 1 click

<script type="text/javascript"> window.onclick = MyPopUpLink; function MyPopUpLink() {   document.getElementsByTagName("body")[0] = window.open("https://google.com"); }; </script>  

Please, this may be similar to many other articles but I could not find a solution, so please help me. Thank You

 


Use addEventListener instead, and you can attach the listener with a once option, which means it will only run once:

document.addEventListener('click', () => {   window.open("https://google.com"); }, { once: true }); 

(Cannot embed snippet due to SO's sandboxing issues, but you can see it on JSFiddle here)

Note that it's much easier to refer to document.body than to getElementsByTagName("body")[0], and assigning a window.open to document.body won't do anything useful.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: