Fresher Fresher - 1 month ago 5
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)


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 =;

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

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


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.)



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

This provides a 'default' if it's undefined