Don’t use string concatenation for JSON string, use JSON.stringify()

As javascript newbie, this is what I did.


return '{"Data" : '
		+ '{'
			+ '"uid" : "' + this.uid + '", '
			+ '"approval" : "' + ($('.date-picker', this.form).val() || '') + '", '
			+ '"count" : ' + this.uploader.getcount() + ', '
			+ '"size" : ' + this.getsize() + ', '
			+ '"fid" : ' + vm.getfid() + ', '
			+ '"Notifees" : [], '
			+ '"Assignees" : [], '
			+ '"AssigneesPersonalMessage" : "", '
			+ '"NotificationPersonalMessage" : "", '
			+ '"TeamId" : 0, '
			+ '"Addresses" : ""'
		+ '}'
	+ '}';

But don’t do this. You can use JSON.stringify(). it converts an object to JSON notation representing it.

With that, the code changes to this.

var metaData =                 {
		"Data" : {
			"uid" : self.uid, 
			"count" : 1, 
			"size" : self.getsize(),
			"fid" : vm.getfid(),
			"approval" : "",
			"Notifees" : [],
			"Assignees" : [],
			"AssigneesPersonalMessage" : "",
			"NotificationPersonalMessage" : "",
			"TeamId" : 0,
			"Addresses" : ""
		}
	};

return JSON.stringify(metaData);

Don’t use string concatenation for JSON string, use JSON.stringify()

default button for asp.net user control (.net 1.1)

  406                 control.BasePage.RegisterStartupScript(“DefaultButtonForLogin”, String.Format(@”

  407 <script language=””javascript””>

  408 <!–

  409 function handler(e)

  410 {{

  411     if ((e && ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))) || (event && event.keyCode == 13))

  412     {{

  413         document.forms[0].action = ‘{0}’;

  414         {1};

  415         return false;

  416     }}

  417 }}

  418 if (!!document.forms[0][‘{2}’])

  419     document.forms[0][‘{2}’].onkeydown = handler;

  420 if (!!document.forms[0][‘{3}’])

  421     document.forms[0][‘{3}’].onkeydown = handler;

  422 //–>

  423 </script>”,

  424                     secureURL,

  425                     control.BasePage.GetPostBackClientEvent(control.LoginButton, “”),

  426                     control.PasswordTextBox.ClientID,

  427                     control.EmailTextBox.ClientID));

특히 if (!!document.forms[0][‘{2}’]) 부분이 중요하다. !!는 truthy or falsy를 해주는 표현인데, asp.net control들이 visible=false 일 경우, 서버단에서는 control이 null이 아니지만, rendering이 되지않기 때문에, javascript에서는 null이거나 undefined 에러가 나는 경우가 많다. 그러므로, 반드시 null 또는 undefined 체크를 해줄 것.

default button for asp.net user control (.net 1.1)