Is this one of the drawbacks of unobtrusive javascript?

Adding embedded javascripts to html tags (e.g. onclick, onsubmit ... etc.) is considered bad practice.

<form id="form1" onsubmit="return validate();"> ... </form> <a id="link1" href="http://www.google.com/" onclick="return popup();">Link1</a> 

But if we do this as shown below, is it possible that the validate or popup function will not be called because the user interacts with the page before calling the ready function?

 <form id="form1"> ... </form> <a id="link1" href="http://www.google.com/">Link1</a> <script type="text/javascript"> $(document).ready( function(){ $('#form1').submit( //validate function(){...} ); $('#link1').click( //popup function(){...} ); } ); </script> 
+4
source share
2 answers

You can always disable them and then use the ready event to enable them only after the events have been connected.

Note that onxxx in HTML may try to access things that don't already exist if you try to cross the DOM in them, which probably causes errors and worse than the user is waiting for a split second.

+1
source

If your method is in an external file, then the form may be available before the script loads, no matter how you attach this method. This is a small compromise to simplify maintenance and share problems.

+1
source

Source: https://habr.com/ru/post/1443698/


All Articles