Javascript onload事件函数仅在服务器重新启动时起作用

Javascript onload event function is working only on server restart

本文关键字:服务器 重新启动 起作用 onload 事件 函数 Javascript      更新时间:2023-09-26

我用Javascript编写了一个函数,该函数将在页面加载时激发。该函数首次运行良好。但如果我在访问其他页面后返回索引页面,它就不能正常工作。它确实在某一点上正常工作,但在那之后跳过代码。

以下是我的功能

       <script>function populate() {  
                    //alert("The Flag is "+$('#flag').val());  
                    val =document.getElementById('flag').value;  
                    xml =document.getElementById('xml').value;  
                    alert(xml);  
                    if (val === "M") {  
                        if (window.ActiveXObject) {  
                            doc = new ActiveXObject('Microsoft.XMLDOM');  
                            doc.async = 'false';  
                            doc.loadXML(xml);  
                            alert("ActiveX");  
                        } else {  
                            var parser = new DOMParser();  
                            doc = parser.parseFromString(xml, 'text/xml');  
                        //  alert("DOMparser");  
                        }  
                        alert("Value true");  
                                                   /* upto here function works correctly each time 
                                                    * I have also seen the values of both val and xml are coming correctly 
                                                   */  
                        passportNo = doc  
                                .getElementsByTagName('PASSPORT_NO')[0].childNodes[0].nodeValue;  
                        //alert('passportNo ' + passportNo);  
                        document.getElementById('passportNo').value = passportNo;  
                        pass_type = doc.getElementsByTagName('PASS_TYPE')[0].childNodes[0].nodeValue;  
                        //  alert("Pass_type = " + pass_type);  
                        if (pass_type === "I") {  
                            document.getElementById('in').checked = true;  
                        } else if (pass_type === "O") {  
                            document.getElementById('out').checked = true;  
                        }  
                        jobNo = doc.getElementsByTagName('JOB_NO')[0].childNodes[0].nodeValue;  
                        //alert("jobNo = "+jobNo);  
                        document.getElementById('job_no').value = jobNo;  
                        jobDt = doc.getElementsByTagName('JOB_DT')[0].childNodes[0].nodeValue;  
                        //alert("jobDT "+jobDt);  
                        document.getElementById('DT').value = jobDt;  
                        //Clear xml  
                        nationality =doc.getElementsByTagName('NATIONALITY')[0].childNodes[0].nodeValue;  
                        document.getElementById('nationality2').value = nationality;  
                        element = document.getElementById('nationality');  
                        element.value = nationality;  
                    }  
                } </script> `

这就是我所说的

<body onload="populate()">
<table width="1270" align="center">
    <tr>
        <td width="1010" height="46" colspan="3" align="center"><h1>Currency
                Declaration Form</h1></td>
    </tr>
</table>
<input type="hidden" id="flag" value="<%=code%>" />
<input type="hidden" id="xml" value="<%=xml%>" />
<form name="myForm" action="Entry.do" method="post"
    onsubmit="return validateAll()" class = "autocompleteOff">
    <table width="1042">
        <tr class="heading">
        </tr>
        <tr>
            <td width="256" align="left"><input type="radio" name="inout"
                id="in" value="I" /> <label>INCOMING </label> <input type="radio"
                name="inout" id="out" value="O" /> <label>OUTGOING </label></td>
            <td width="774" align="right"><label>JobNo/DT</label> <input
                type="text" name="job_no" id="job_no" readonly="readonly"
                tabindex="-1" /> <input type="text" name="DT" id="DT"
                readonly="readonly" tabindex="-1" value="<%=Convert.getSysDate()%>" /></td>
        </tr>
    </table>`

我在HTML代码中既看不到passportNo id也看不到PASSPORT_NO标记(getElementsByTagName)。pass_typenationality和许多其他元件也存在同样的问题。你错过了一些代码吗?或者,这可能是PHP的动态输出(例如),并且在第一次运行后返回不同的HTML?