chrome调试javascript详解(2)

var list = document.querySelectorAll('div.rtmarg'); console.assert(list[0].childNodes.length > 10 , "Oops,this is small"); function login(user) { console.count("Login called for user '" + user + "'"); } login("join"); login("join"); login("join"); login("chen"); console.dir(document.body); function connectToServer() { var errorCode = 1; if (errorCode) { console.error("Error: %s (%i)", "Server is not responding", 500); } } connectToServer(); var list = document.querySelectorAll("div.rtmarg"); console.dirxml(list[0]);

chrome调试javascript详解

6、Other Command Line API

inspect(document.body.firstChild); getEventListeners(document); var player1 = { "name": "Ted", "level": 42} keys(player1); function sum(x, y) { return x + y;} monitor(sum); monitorEvents(window, "resize");

chrome调试javascript详解

7、debugger 非常好用的一个工具

brightness = function() { debugger; var r = Math.floor(this.red*255); var g = Math.floor(this.green*255); var b = Math.floor(this.blue*255); return (r * 77 + g * 150 + b * 29) >> 8; } brightness();

chrome调试javascript详解


调试的时候还可以加断点什么的……

8、jquery相关  firequery

$.fn.log = function() { if (window.console && console.log) { console.log(this); } return this; } $('foo.bar').find(':baz').log().hide();

这样就可以 easily check inside jQuery chains.

chrome调试javascript详解

四、相关资源

Firefox

(you can also now use Firefox's built in developer tools Ctrl+Shift+J (Tools > Web Developer > Error Console), but Firebug is much better; use Firebug)
Safari and Chrome
Basically the same.
https://developer.chrome.com/devtools/index
https://developer.apple.com/technologies/safari/developer-tools.html
Internet Explorer
Don't forget you can use compatibility modes to debug IE7 and IE8 in IE9 or IE10
(v=vs.85).aspx
(v=vs.85).aspx
If you must access the console in IE6 for IE7 use the Firebug Lite bookmarklet
look for stable bookmarklet

Opera

iOS
Works for all iPhones, iPod touch and iPads.

Now with iOS 6 you can view the console through Safari in OS X if you plug in your device. Or you can do so with the emulator, simply open a Safari browser window and go to the "Develop" tab. There you will find options to get the Safari inspector to communicate with your device.
Windows Phone, Android
Both of these have no console built in and no bookmarklet ability. So we use :listen and it will give you a script tag to place in your HTML. From then on you can view your console inside the jsconsole website.
iOS and Android
You can also use to access web inspector tools and the console on any device using their convenient browser plugin.
Older browser problems
Lastly older browsers (thanks again Microsoft) will crash if you use console.log in your code and not have the developer tools open at the same time. Luckily its an easy fix. Simple use the below code snippet at the top of your code and good old IE should leave you alone:
 if(!window.console){ window.console = {log: function(){} }; }
This checks to see if the console is present, and if not it sets it to an object with a blank function calledlog. This way window.console and window.console.log is never truly undefined.


https://developers.google.com/chrome-developer-tools/docs/console-api

https://developer.chrome.com/devtools/docs/console-api
https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html
 https://developer.mozilla.org/zh-CN/docs/Web/API/Console

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggyzw.html