Javascript空数组对象和url参数

时间:2016-09-13 16:41:55

标签: javascript html asp.net

我有这个脚本,它解码url并使用url参数设置asp字段的值(?lastname = Somebody& phone = 12345)

        <script>

var urlParams;
(window.onpopstate = function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();

    lastname = urlParams["lastname"];
   document.getElementById('ctl00_maincontent_FormView1_FirstNameTextBox').value = lastname;

        </script>

如何检查数组元素是否为空,只是将该字段留空,而现在我得到了未定义。我有多个要检查的参数(姓氏,姓名,电话等)

感谢。

1 个答案:

答案 0 :(得分:0)

你可以做类似的事情 lastname = urlParams["lastname"] || '';

这提供了一个&#39;默认&#39;如果它未定义