輸入Jsonp網址即時檢視Json內容架構。
原始碼:
<input type="text" id="dumpvalue" value="http://...&alt=json&callback=?" /> <button id="dumpbtn">Dump</button> <pre id="gplus"></pre> <script type="text/javascript"> function $(id) { var key = id.substr(0,1); switch(key) { case '#': return document.getElementById(id.substr(1)); case '.': var elems = document.body.getElementsByTagName("*"); var target = id.substr(1); var result=[]; for (i=0;j=elems[i];i++) { if((j.className).indexOf(target)!=-1) result.push(j); } return result; default: return document.getElementsByTagName(id); } } var dumpboxy = new function() { return { getjson : function(json) { $('#gplus').innerHTML = this.dump(json); }, sendjson : function(url, func) { var cp = url.indexOf("callback=?"); var rm = $('#scriptBoxy'); if(rm) rm.parentNode.removeChild(rm); var script = document.createElement('script'); script.type = 'text/javascript'; script.id = 'jsonScript'; script.src = url.substring(0, cp)+"callback="+func+url.substring(cp+10, url.length); document.getElementsByTagName('head')[0].appendChild(script); }, dump : function(arr, level) { var dumped_text = ""; if(!level) level = 0; var level_padding = ""; for(var j=0;j<level+1;j++) level_padding += "\t"; if(typeof(arr) == 'object') { for(var item in arr) { var value = arr[item]; if(typeof(value) == 'object') { dumped_text += level_padding + "'" + item + "' ...\n"; dumped_text += this.dump(value, level+1); } else { dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; } } } else { dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; } return dumped_text; } }; } $('#dumpbtn').onclick = function(e) { dumpboxy.sendjson($('#dumpvalue').value, "dumpboxy.getjson"); }; $('#dumpvalue').onclick = function(e) { $('#dumpvalue').select(); }; </script>
1 則留言:
張貼留言