Fresher Fresher - 2 months ago 6
ASP.NET (C#) Question

Javascript empty array object and url parameter

I have this script, which decodes the url and set the value of the asp field with url`s parameter (?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>


How to check if an array element is null and just leave the field blank, instead now i get undefined. I have more than one parameter to check (lastname, name, phone, ect.)

Thanks.

Answer

You could do something like
lastname = urlParams["lastname"] || '';

This provides a 'default' if it's undefined

Comments