views:

112

answers:

4

In the HTML head section:

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

Just above the </body> tag(closing tag, bottom of the html page). Also: this is the old code, this is how it was when it was not working:

    <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 would not work. I replace the javascript in the footer with this.
This is the new code, which does work as I expect it to.:

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

And now it does work... I don't see how leaving out that function call, even though it the function it was referring to doesn't exist, would mess things up so royally.

+1  A: 

probably because you arent calling your scripts on document load event. so when you called your scripts in the header before your dom fully loaded, none of it worked, but now when you are calling it after the dom loads, it works.

The correct fix for all of this should be calling your scripts after the document fully loads, or at least from the body onload event:

<body onload="initScripts()">

And then add all of the scripts you want to run on page load in the initScripts function.

also, there are much better ways of doing this, for example using jquery, and/or reading this: http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html

mkoryak
Wait a second, both the old and new javascript were placed at the bottom of the html. The difference is that I left out 1 function call.
WebDevHobo
+4  A: 

In an unbracketed if statement, only the first statement is conditional. Every statement following it is unconditional regardless of indentation.

Thus, in the first example, loadevents() executed unconditionally.

unknown
A: 

You say: "I don't see how leaving out that function call, even though it the function it was referring to doesn't exist, would mess things up so royally." That's inconsistent with the rest of your question, which implies that adding the call messed things up. But I think the text I'm quoting is the correct description.

Here's the real answer. The old code:

if(document.getElementById)loadEvents();

does not call loadEvents if getElementsById is not defined. It's not defined in all browsers.

The new code, instead, you not only leave out the function call: the semantics change as well.

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

always calls loadEvents, so what you want to happen always does.

redtuna
actually, you switched old and new.
WebDevHobo
If I guessed the wrong fix for the question's ambiguity (i.e. it's in fact *adding* a function call that messes things up royally, not leaving one out as the question states now), then I have no idea why the new code works and the old one doesn't.
redtuna
Someone came along and explained it. I marked it as the correct answer. Have a read if you're still interested.
WebDevHobo
+1  A: 

The browser would have reported an error when attempting to call the "initialize" function since there was no such function. Therefore, the very next line where you call "loadEvents" wouldn't run. See this example:

<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>

In that example, the alert box shouldn't appear because I haven't declared an "initialize" function and the browser will report a JS error. Removing the "initialize" function, however, will cause the alert box to appear.

So that's how by removing the cause of the Javascript error you fixed your problem.

Joe D