I am writing at the beginning of the html document:

 <script src=js/JS_req.js></script> 

Below I call the function:

 <button type=submit class="btn btn-primary" data-dismiss=modal onclick = 'login_1()'>Увійти</button> 

I will announce in the js file:

 function login_1() { alert("Авторизация временно недоступна")}; 

And debager writes:

Uncaught ReferenceError: login_1 is not defined

(swears at the line where I called it by onClick ).

    4 answers 4

    Maybe the browser has cached the old js/JS_req.js ? And it is better to quote the file name and other values ​​of HTML attributes. Not necessarily, but better.

    It is considered an unsuccessful practice to write handlers directly in the body of HTML, it is better to hang event listeners:

     <button id="btn-exit" type="submit" class="btn btn-primary" data-dismiss="modal"> Увійти </button> // ... <script> document .getElementById('btn-exit') .addEventListener('click', function(){ alert("Авторизация временно недоступна"); }) ; </script> 

    In this embodiment, the script must be connected at the end of the document, before the closing </body> .

    • As I understand it, you still need to push the script into the html file, but I also have heavy scripts that you don’t want to load onto the client - Kernozz
    • At the end (and not at the beginning) of the document, connect an external script - as before, only transfer it to the end. Because you first need to have exactly the button with the desired id in the document, and only then attach the event listener to it. - Sergiks
    • As the shortest solution to your original problem, just try to add a random parameter to the script address so that it is updated from the server exactly: <script src="js/JS_req.js?42" ... - Sergiks

    You can try to add an id to your button, for example, id = "login", and then add a click handler.

     <script> var button = document.getElementById("login"); button.onclick = function() { // ... }; </script> 

      Likely, you use function BEFORE the script was loaded. Browser stumbles upon

       <script src=js/JS_req.js></script> 

      sends a request to the server and drove further to analyze the code. Moreover, the code is very quickly analyzed and executed. Therefore, the browser manages to “run through” the code to the function call, but the script has not yet been loaded with it and, therefore, has not been analyzed - your function has not yet been entered by the preloadscanner into the corresponding code execution context object.

      On the move, I came up with such an option - to make a set interval, which will check if the login_1 () function is defined every few (for example, 100) milliseconds. if defined, it is executed and the interval is deleted.

        Add <script src=js/JS_req.js></script> at the end of the document (before the closing </body> ).

        • These tips won't help with inline handlers. - Grundy
        • @Grundy, thanks, now I will know) - HamSter pm
        • not, at the end of the document also does not help, because with inline handlers, the function must be declared before use - Grundy