Javascript出错后继续运行代码

时间:2018-11-16 01:32:05

标签: javascript sql-server deployment

我正在对一些值进行测试,以查看给出正确的输出。

每次更改代码时,我都需要重新部署到服务器,这会影响服务器上的并发用户。

我希望将其最小化。

即使发现错误,是否可以继续运行代码?

Javascript:

var schedule = document.getElementById("<%=ddlExecutionSchedule.ClientID%>").value;
console.log("schedule is " + schedule);  //returns UNDEFINED

var schedule2 = document.getElementById("<%=ddlExecutionSchedule.ClientID%>").length;
console.log("schedule2 is " + schedule2); //returns UNDEFINED

var schedule3 = document.getElementById('ddlExecutionSchedule'); 
console.log("schedule3 is " + schedule3); //returns HTML OBJECT

var schedule4 = document.getElementById('ddlExecutionSchedule').value;
console.log("schedule4 is " + schedule4); //returns UNDEFINED

var schedule5 = document.getElementById('ddlExecutionSchedule').length;
console.log("schedule5 is " + schedule5); //returns UNDEFINED

var schedule6 = document.getElementById(<%=ddlExecutionSchedule.ClientID%>);
console.log("schedule6 is " + schedule6); //returns NULL

var options = schedule.getElementsByTagName('input');
//^ = The line above caught an error of <Uncaught TypeError: 
      Cannot read property 'getElementsByTagName' of undefined

var options2 = schedule2.getElementsByTagName('input');
//^ = To test

...

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

trycatch会有所帮助:

var schedule1 = document.getElementById("id1").length;
console.log("schedule1 is " + schedule1); //returns UNDEFINED

var schedule2 = document.getElementById("id1");
console.log("schedule2 is " + schedule2);

try {
  var options1 = schedule1.getElementsByTagName('input');
  console.log('options1: ' + options1);
} catch (err) {
  console.log('error: '+ err.message);
}
var options2 = schedule2.getElementsByTagName('input');
console.log('options2: ' + options2);
<label id="id1"><input></label>