본문 바로가기

프로그래밍/Javascript

php의 var_dump처럼 객체의 모든 정보를 보여주는 javascript 함수

/**
*   속성을 보고싶은 객체를 전달하면 해당 객체가 가지고 있는 모든 속성을 새창을열고 보여준다
*   @param   obj : 속성을 보고싶은 객체
*   @return   void
*/
function var_dump(obj, isSub) {
 try {
  var header = '';
  var footer = '';

  if (!isSub) {
   header = '<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />\n<style>table {width:100%;background-color:#CCCCCC;border-width:0px;border-collapse: collapse;} th {background-color:#EFEFEF;color:#7B7B7B;} td {background-color:#FFFFFF;} th, td {border:1px #CCCCCC solid;}</style>\n<title>무제 문서</title>\n<body>\n';
   footer = '</body></html>';
  }

  var msg = header + '<table>';

  for (var property in obj) {
   if ($type(obj[property]) == 'object')
    msg = msg + '<tr><th>' + property + '</th><td>' + var_dump(obj[property], true) + '</td></tr>\n';
   else
    msg = msg + '<tr><th>' + property + '</th><td>' + obj[property] + '</td></tr>\n';
  }
  msg = msg + '</table>\n' + footer;
  if (!isSub) {
   var new_info = window.open('', 'new_info');
   new_info.document.clear();
   new_info.document.write(msg);
  } else {
   return msg;
  }

 } catch(e) {
  window.status = 'Error: ' + e.number +'; ' + e.description;
 }
}