The problem with Javascript is resolved, I don’t understand what the problem is

In the HTML header section:

<script type="text/javascript" src="Scripts/editScripts.js"></script>

Just above the tag </body>(closing tag, at the bottom of the html page). Also: this is old code, and it was when it didn't work :

    <script type="text/javascript">if(document.getElementById)initialize();loadEvents();</script>
  </body>
</html>  

In the editScripts.js file:

/*global document,addFileInput*/
function loadEvents() {
    var a = document.getElementById('addField');
    a.onclick = addFileInput;
}
var upload_number = 2;
function addFileInput() {
    var d = document.createElement("div");
    var file = document.createElement("input");
    file.setAttribute("type", "file");
    file.setAttribute("name", "addFile[]");
    file.setAttribute("size", "35");
    file.setAttribute("class", "file");
    file.setAttribute("id", "addFile"+upload_number);
    d.appendChild(file);
    document.getElementById("moreUploads").appendChild(d);
    upload_number++;
}

This will not work. I am replacing javascript in the footer with this.
This is new code that works as I expect. :

<script type="text/javascript">if (document.getElementById)loadEvents();</script>

And now it will work ... I don’t see how refusing to call this function, even if it is the function that it referred to, does not exist, will randomly get confused.

+3
source share
4 answers

"initialize" , . , , loadEvents, . . :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JS Error Test</title>
    </head>
    <body>
        <script type="text/javascript">
            if(document.getElementById) {
                initialize();
                alert("You shouldn't see me!");
            }
        </script>
    </body>
</html>

, "initialize" , JS. "initialize" .

, Javascript, .

+1

if . .

, loadevents() .

+4

, , . , , , , , dom, .

, , onload body:

<body onload="initScripts()">

, initScripts.

, , , jquery / : http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html

+1

: " , , , , , ". , , . , , , .

. :

if(document.getElementById)loadEvents();

loadEvents, getElementsById . .

: .

if(document.getElementById)initialize();loadEvents();

loadEvents, , , .

0

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


All Articles