...

View Full Version : JavaScript equivalent of PHP print_r()



bauhsoj
08-20-2005, 12:32 AM
If there a JavaScript equivalent of PHP's print_r() or vardump()? This would be very handy for debugging.

martin_narg
08-20-2005, 01:21 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function var_dump(obj) {
if(typeof obj == "object") {
return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
} else {
return "Type: "+typeof(obj)+"\nValue: "+obj;
}
}

/* usage */
window.onload = function() {
var myString = "hello world";
alert(var_dump(myString));

var myInteger = 10;
alert(var_dump(myInteger));

var myArray = new Array(1,2,3);
alert(var_dump(myArray));

alert(var_dump(window));
}
</script>
</head>

<body>
</body>
</html>


Just an idea, hope it helps.

m_n

bauhsoj
08-20-2005, 06:45 PM
Much thanks! Very close to what I was looking for. :thumbsup:

qywter
05-22-2008, 07:54 AM
http://code.google.com/p/jsvardump/

A1ien51
05-22-2008, 12:58 PM
you can also use Firebug [www.getFirebug.com]

Eric

rubs33
03-13-2011, 09:46 PM
I have created a JavaScript code that is very similar to PHP's var_dump:
http://rubsphp.blogspot.com/2011/03/vardump-para-javascript.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum