dongyidao1461 2016-04-25 19:18
浏览 181

ReferenceError:函数未定义。 我错过了什么?

Can't seem to figure out why I keep getting this ReferenceError: OnLoad is not defined error.

Since the time of my previous commit, I have changed lines 28-30 and that is all.

How can this cause my javascript to not be loaded properly? I have only changed these three lines. I'm certain these lines shouldn't really be related. The Javascript file is identical to the one in the previous commit. What am I missing?

UserInterface.php - Current Commit

class UserInterface {
    var $ParentAppInstance;
    function __construct($AppInstance){
        $this->ParentAppInstance = $AppInstance;
        $this->DrawPageHTML();
        $this->DrawDBSetDropdown();
        $this->DrawQueryForm();
     }


//Override thjis function to change the HTML and PHP of the UI page.
protected function DrawPageHTML(){
    $CurrDB_Obj = $this->ParentAppInstance->CurrentDBObj; //Line 28
    $EncodedFields = json_encode($CurrDB_Obj->GetFields()); //Line 29
    echo "<body onload='OnLoad($EncodedFields);'>"; // Line 30
    echo '
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="./CSS/UserInterface.css">
        <div id="DebugOutput"></div>
    </body>


    ';

}

protected function DrawDBSetDropdown(){
    echo '<div align="right">';
        echo '<select onchange="SwitchDatabaseSet();" name="DBSetList" form="DBSetSelector" id="DBSetSelector">';
        $i = 0;
        foreach ($this->ParentAppInstance->DBSetsArr as $DBSet){
            if ($DBSet->DBSetName == $this->ParentAppInstance->CurrDBSetStr){
                echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
            }
        }

        foreach ($this->ParentAppInstance->DBSetsArr as $DBSet){
            if ($DBSet->DBSetName == $this->ParentAppInstance->CurrDBSetStr){/* DO NOTHING. IE. IGNORE IT*/}
            else if ($DBSet->DBSetName == 'DBSet0'){/* DO NOTHING. IE. IGNORE IT*/}
            else{
                //Add the DBSet to the dropdown list.
                $i++;
                echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
            }
        }
        echo '</select>';
    echo '</div>';

}

protected function DrawQueryForm(){
    echo '<form action="DatabaseSearch.php" method="post" accept-charset="UTF-8">';
        echo '<div id="QFormBody">';
        $NumActiveQBoxes = $this->ParentAppInstance->Config['ApplicationSettings']['NumberDefaultQueryOptions'];
        for ($i = 1; $i <= $NumActiveQBoxes; $i++){
            echo '<div class="QueryBox" name="QBox_' . $i . '">';
                echo '<select name=Field_' . $i . '">';
                    $DBSet_Num = filter_var($this->ParentAppInstance->CurrDBSetStr, FILTER_SANITIZE_NUMBER_INT);
                    $CurrDBSet_Obj = $this->ParentAppInstance->DBSetsArr[$DBSet_Num];
                    foreach($CurrDBSet_Obj->GetDBSetFields() as $Field){
                        //echo $Field;
                        echo '<option>' . $Field . '</option>';
                    }
                echo '</select>';
                echo '<input type="text" name="Query_' . $i . '"></input>';
                echo '<button class= "RMButton" type="button">-</button>';
            echo '</div>';
        }
        echo '<button type="button" id="add" onclick="AddQueryBox();">+</button>';
        echo '<button type="submit" id="submit">SEARCH</button>';
    echo '</Form>';


    echo '<script src=/GLS_DBSearchProject/JavaScript/UserInterface.js></script>';


}

UserInterface.php - Previous Commit

class UserInterface {
var $ParentAppInstance;
function __construct($AppInstance){
    $this->ParentAppInstance = $AppInstance;
    $this->DrawPageHTML();
    $this->DrawDBSetDropdown();
    $this->DrawQueryForm();
}


//Override thjis function to change the HTML and PHP of the UI page.
protected function DrawPageHTML(){
    $DBSet_Num = filter_var($this->ParentAppInstance->CurrDBSetStr, FILTER_SANITIZE_NUMBER_INT);    //Line 28
    $CurrDBSet_Obj = $this->ParentAppInstance->DBSetsArr[$DBSet_Num];     //Line 29
    $EncodedFields = json_encode($CurrDBSet_Obj->GetDBSetFields());      //Line 30
    echo "<body onload='OnLoad($EncodedFields);'>";
    echo '
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="./CSS/UserInterface.css">
        <div id="DebugOutput"></div>
    </body>


    ';

}

protected function DrawDBSetDropdown(){
    echo '<div align="right">';
        echo '<select onchange="SwitchDatabaseSet();" name="DBSetList" form="DBSetSelector" id="DBSetSelector">';
        $i = 0;
        foreach ($this->ParentAppInstance->DBSetsArr as $DBSet){
            if ($DBSet->DBSetName == $this->ParentAppInstance->CurrDBSetStr){
                echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
            }
        }

        foreach ($this->ParentAppInstance->DBSetsArr as $DBSet){
            if ($DBSet->DBSetName == $this->ParentAppInstance->CurrDBSetStr){/* DO NOTHING. IE. IGNORE IT*/}
            else if ($DBSet->DBSetName == 'DBSet0'){/* DO NOTHING. IE. IGNORE IT*/}
            else{
                //Add the DBSet to the dropdown list.
                $i++;
                echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
            }
        }
        echo '</select>';
    echo '</div>';

}

protected function DrawQueryForm(){
    echo '<form action="DatabaseSearch.php" method="post" accept-charset="UTF-8">';
        echo '<div id="QFormBody">';
        $NumActiveQBoxes = $this->ParentAppInstance->Config['ApplicationSettings']['NumberDefaultQueryOptions'];
        for ($i = 1; $i <= $NumActiveQBoxes; $i++){
            echo '<div class="QueryBox" name="QBox_' . $i . '">';
                echo '<select name=Field_' . $i . '">';
                    $DBSet_Num = filter_var($this->ParentAppInstance->CurrDBSetStr, FILTER_SANITIZE_NUMBER_INT);
                    $CurrDBSet_Obj = $this->ParentAppInstance->DBSetsArr[$DBSet_Num];
                    foreach($CurrDBSet_Obj->GetDBSetFields() as $Field){
                        //echo $Field;
                        echo '<option>' . $Field . '</option>';
                    }
                echo '</select>';
                echo '<input type="text" name="Query_' . $i . '"></input>';
                echo '<button class= "RMButton" type="button">-</button>';
            echo '</div>';
        }
        echo '<button type="button" id="add" onclick="AddQueryBox();">+</button>';
        echo '<button type="submit" id="submit">SEARCH</button>';
    echo '</Form>';


    echo '<script src=/GLS_DBSearchProject/JavaScript/UserInterface.js></script>';


}

UserInterface.js

var DBSetFields = [];
var NumQBoxes = 3;



//window.onload = OnLoad();
function OnLoad(Fields){
    console.log("OnLoad called");
    CloneDBSetFields(Fields);

    var RMNodeList = document.getElementsByClassName('RMButton');
    for (var i = 0; i < RMNodeList.length; ++i) {
        console.log(RMNodeList[i]);
        RMNodeList[i].onclick = RemoveQBox;  // Calling myNodeList.item(i) isn't necessary in JavaScript
    }
}

function JSTEST(){
    window.alert("JS Called Successfully!!");
}


function CloneDBSetFields(Fields){
    console.log("CloneDBSetFields");
    DBSetFields = Fields;


}

function SwitchDatabaseSet(MainPageDoc){
    document.getElementById("DebugOutput").innerHTML = "Test";
    window.location.replace('/GLS_DBSearchProject/index.php?DBSet=' + document.getElementById("DBSetSelector").value);
    console.log(document.getElementById("DBSetSelector").value);
    //console.log(document.)

}

function Fields_FOREACH(ELEMENT, INDEX, ARRAY){
    console.log("TEST");
    var FieldOption = document.createElement('option');
    FieldOption.setAttribute('value', ARRAY[INDEX]);
    FieldOption.innerHTML = ARRAY[INDEX];
    this.appendChild(FieldOption);
}

function AddQueryBox(){
    NumQBoxes += 1;
    var NewQBox = document.createElement('div');
    NewQBox.setAttribute('class', 'QueryBox');

    //Create and fill Field Selector dropdown "select" element
    var FieldSelector = document.createElement('select');
    FieldSelector.setAttribute('name', 'Field_' + NumQBoxes);
    //foreach element in Fields
    console.log(DBSetFields);
    DBSetFields.forEach(Fields_FOREACH, FieldSelector);
    //Create and fill 
    var QueryText = document.createElement('input');
    QueryText.setAttribute('type', 'text');
    QueryText.setAttribute('name', 'Query_' + NumQBoxes);

    //Create "-" Remove button for removing query lines.
    var RemoveButton = document.createElement('button');
    RemoveButton.innerHTML = "-";
    RemoveButton.setAttribute('type', 'button');
    RemoveButton.setAttribute('class', 'RMButton');
    RemoveButton.addEventListener("click", RemoveQBox);

    //Combine the individual elements into a new query box and insert the new query box into the HTML Document
    NewQBox.appendChild(FieldSelector);
    NewQBox.appendChild(QueryText);
    NewQBox.appendChild(RemoveButton);
    document.getElementById("QFormBody").insertBefore(NewQBox, document.getElementById("add"));

}

function RemoveQBox(e){
    console.log("Remove");
    var RemoveButton = this; //this == e.currentTarget
    console.log(RemoveButton);
    var QBox = RemoveButton.parentNode;
    QBox.remove();
    NumQBoxes -= 1;
}

EDIT: My Javascript file is not being loaded on the client side (ie. it doesn't show up under "Sources", so I'm not really not sure: Why wouldn't my javascript be loading on the client side?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起