Firefox没有在javascript中捕获“未定义”错误

时间:2014-08-28 10:56:35

标签: javascript firefox

我有一些javascript代码,它根据SELECT列表中的选定选项调用带有参数的servlet。 selectCampaigns(account)可能没有选项,因此有trycatch

如果,{/ p>,则应调用catch中的代码

var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;

失败。这适用于chrome和IE,但在firefox中它提供TypeErrror

TypeError: selectCampaigns.options[selectCampaigns.selectedIndex] is undefined

不会触发catch。我可以就如何处理这个问题提出一些建议吗? (另外firefox没有显示我为调试插入的任何警报)。

function selectcampaign(account) {
    alert('alert1');
    var selectCampaigns = document.getElementById("campaignSelect");
    var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart";

    try {
        var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
        urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account +   "&campaign=" + selectedCampaign;
    } catch(err) {
        alert(err);
        urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
    } finally {
        window.location.href = urlstrg;
    }
};

1 个答案:

答案 0 :(得分:0)

您应该更好地测试值而不是处理错误。

function selectcampaign(account) {
    var selectCampaigns = document.getElementById("campaignSelect");
    var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;

    if(selectCampaigns.options[selectCampaigns.selectedIndex]) {
        urlstrg += "&campaign=" + selectCampaigns.options[selectCampaigns.selectedIndex].text;
    }

    window.location.href = urlstrg;
};