browser-debugging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBrowser Debugging
浏览器调试
Overview
概述
Browser debugging tools help identify and fix client-side issues including JavaScript errors, layout problems, and performance issues.
浏览器调试工具可帮助识别和修复客户端问题,包括JavaScript错误、布局问题和性能问题。
When to Use
适用场景
- JavaScript errors
- Layout/styling issues
- Performance problems
- User interaction issues
- Network request failures
- Animation glitches
- JavaScript错误
- 布局/样式问题
- 性能问题
- 用户交互问题
- 网络请求失败
- 动画故障
Instructions
操作步骤
1. Browser DevTools Fundamentals
1. 浏览器DevTools基础
yaml
Chrome DevTools Tabs:
Elements/Inspector:
- Inspect HTML structure
- Edit HTML/CSS in real-time
- View computed styles
- Check accessibility tree
- Modify DOM
Console:
- View JavaScript errors
- Execute JavaScript
- View console logs
- Monitor messages
- Clear errors
Sources/Debugger:
- Set breakpoints
- Step through code
- Watch variables
- Call stack view
- Conditional breakpoints
Network:
- View all requests
- Check response status
- Inspect headers
- View response body
- Throttle network
Performance:
- Record runtime
- Identify bottlenecks
- Flame charts
- Memory usage
- Frame rate
Memory:
- Heap snapshots
- Memory growth
- Object allocation
- Detect leaks
---
Essential Shortcuts:
F12 / Ctrl+Shift+I: Open DevTools
Ctrl+Shift+C: Element inspector
Ctrl+Shift+J: Console
Ctrl+Shift+K: Console (Firefox)yaml
Chrome DevTools Tabs:
Elements/Inspector:
- Inspect HTML structure
- Edit HTML/CSS in real-time
- View computed styles
- Check accessibility tree
- Modify DOM
Console:
- View JavaScript errors
- Execute JavaScript
- View console logs
- Monitor messages
- Clear errors
Sources/Debugger:
- Set breakpoints
- Step through code
- Watch variables
- Call stack view
- Conditional breakpoints
Network:
- View all requests
- Check response status
- Inspect headers
- View response body
- Throttle network
Performance:
- Record runtime
- Identify bottlenecks
- Flame charts
- Memory usage
- Frame rate
Memory:
- Heap snapshots
- Memory growth
- Object allocation
- Detect leaks
---
Essential Shortcuts:
F12 / Ctrl+Shift+I: Open DevTools
Ctrl+Shift+C: Element inspector
Ctrl+Shift+J: Console
Ctrl+Shift+K: Console (Firefox)2. Debugging Techniques
2. 调试技巧
javascript
// Breakpoints
// Line breakpoint
// Click line number in Sources tab
// Conditional breakpoint
// Right-click line → Add conditional breakpoint
if (userId === 123) {
// Pauses only when userId is 123
}
// DOM breakpoint
// Right-click element → Break on → subtree modifications
// Pauses when DOM changes
// Event listener breakpoint
// Sources tab → Event Listener Breakpoints
// Pauses on specific event
// Debugger statement
function problematicFunction() {
debugger; // Pauses here if DevTools open
// ... rest of code
}
---
Watch Expressions
// Add variable to watch
// Updates as code executes
watch: {
userId: 123,
orders: [],
total: 0
}
Call Stack
// Shows function call chain
main()
-> processUser()
-> validateUser()
-> PAUSED HERE
Step Controls:
Step over: Execute current line
Step into: Enter function
Step out: Exit function
Continue: Run to next breakpointjavascript
// Breakpoints
// Line breakpoint
// Click line number in Sources tab
// Conditional breakpoint
// Right-click line → Add conditional breakpoint
if (userId === 123) {
// Pauses only when userId is 123
}
// DOM breakpoint
// Right-click element → Break on → subtree modifications
// Pauses when DOM changes
// Event listener breakpoint
// Sources tab → Event Listener Breakpoints
// Pauses on specific event
// Debugger statement
function problematicFunction() {
debugger; // Pauses here if DevTools open
// ... rest of code
}
---
Watch Expressions
// Add variable to watch
// Updates as code executes
watch: {
userId: 123,
orders: [],
total: 0
}
Call Stack
// Shows function call chain
main()
-> processUser()
-> validateUser()
-> PAUSED HERE
Step Controls:
Step over: Execute current line
Step into: Enter function
Step out: Exit function
Continue: Run to next breakpoint3. Common Issues & Solutions
3. 常见问题与解决方案
yaml
Issue: JavaScript Error in Console
Error Message: "Uncaught TypeError: Cannot read property 'map' of undefined"
Solution Steps:
1. Note line number from error
2. Click line to go to Sources tab
3. Set breakpoint before error
4. Check variable values
5. Trace how undefined value occurred
Example:
const data = await fetchData();
const items = data.results.map(x => x.name);
// Error if results is undefined
// Add check: const items = data?.results?.map(...)
---
Issue: Element Not Showing (Hidden)
Debug:
1. Right-click element → Inspect
2. Check display: none in CSS
3. Check visibility: hidden
4. Check opacity: 0
5. Check position off-screen
6. Check z-index buried
7. Check parent hidden
---
Issue: CSS Not Applying
Debug:
1. Inspect element
2. View Styles panel
3. Find CSS rule
4. Check if crossed out (overridden)
5. Check specificity
6. Check media queries
7. Check !important usage
---
Issue: Memory Leak
Detect:
1. Memory tab
2. Take heap snapshot
3. Perform action
4. Take another snapshot
5. Compare (delta)
6. Objects retained? (leaked)
7. Check detached DOM nodes
Fix:
- Remove event listeners
- Clear timers
- Release object references
- Cleanup subscriptionsyaml
Issue: JavaScript Error in Console
Error Message: "Uncaught TypeError: Cannot read property 'map' of undefined"
Solution Steps:
1. Note line number from error
2. Click line to go to Sources tab
3. Set breakpoint before error
4. Check variable values
5. Trace how undefined value occurred
Example:
const data = await fetchData();
const items = data.results.map(x => x.name);
// Error if results is undefined
// Add check: const items = data?.results?.map(...)
---
Issue: Element Not Showing (Hidden)
Debug:
1. Right-click element → Inspect
2. Check display: none in CSS
3. Check visibility: hidden
4. Check opacity: 0
5. Check position off-screen
6. Check z-index buried
7. Check parent hidden
---
Issue: CSS Not Applying
Debug:
1. Inspect element
2. View Styles panel
3. Find CSS rule
4. Check if crossed out (overridden)
5. Check specificity
6. Check media queries
7. Check !important usage
---
Issue: Memory Leak
Detect:
1. Memory tab
2. Take heap snapshot
3. Perform action
4. Take another snapshot
5. Compare (delta)
6. Objects retained? (leaked)
7. Check detached DOM nodes
Fix:
- Remove event listeners
- Clear timers
- Release object references
- Cleanup subscriptions4. Performance Debugging
4. 性能调试
yaml
Network Performance:
1. Open Network tab
2. Reload page
3. Identify slow resources:
- Large images (>500KB)
- Large JavaScript (>300KB)
- Slow requests (>2s)
- Waterfall bottlenecks
Solutions:
- Optimize images
- Code split JavaScript
- Lazy load resources
- Compress assets
- Use CDN
Runtime Performance:
1. Performance tab
2. Record interaction
3. Analyze flame chart:
- Long red bars = slow
- Identify functions
- Check main thread blocking
- Monitor frame rate
Solutions:
- Move work to Web Workers
- Defer non-critical work
- Optimize algorithms
- Use requestAnimationFrame
---
Checklist:
Console:
[ ] No errors
[ ] No warnings (expected ones)
[ ] No unhandled promise rejections
Elements:
[ ] HTML structure correct
[ ] CSS applied correctly
[ ] No accessibility issues
[ ] Responsive at all breakpoints
Network:
[ ] All resources load successfully
[ ] No excessive requests
[ ] File sizes reasonable
[ ] No blocked resources
Performance:
[ ] Frame rate >60 FPS
[ ] No long tasks (>50ms)
[ ] LCP <2.5s
[ ] Memory stableyaml
Network Performance:
1. Open Network tab
2. Reload page
3. Identify slow resources:
- Large images (>500KB)
- Large JavaScript (>300KB)
- Slow requests (>2s)
- Waterfall bottlenecks
Solutions:
- Optimize images
- Code split JavaScript
- Lazy load resources
- Compress assets
- Use CDN
Runtime Performance:
1. Performance tab
2. Record interaction
3. Analyze flame chart:
- Long red bars = slow
- Identify functions
- Check main thread blocking
- Monitor frame rate
Solutions:
- Move work to Web Workers
- Defer non-critical work
- Optimize algorithms
- Use requestAnimationFrame
---
Checklist:
Console:
[ ] No errors
[ ] No warnings (expected ones)
[ ] No unhandled promise rejections
Elements:
[ ] HTML structure correct
[ ] CSS applied correctly
[ ] No accessibility issues
[ ] Responsive at all breakpoints
Network:
[ ] All resources load successfully
[ ] No excessive requests
[ ] File sizes reasonable
[ ] No blocked resources
Performance:
[ ] Frame rate >60 FPS
[ ] No long tasks (>50ms)
[ ] LCP <2.5s
[ ] Memory stableKey Points
关键点
- Open DevTools with F12
- Console tab shows JavaScript errors first
- Sources tab for setting breakpoints
- Inspector for HTML/CSS inspection
- Network tab for request analysis
- Performance tab for profiling
- Memory tab for leak detection
- Use conditional breakpoints for debugging
- Monitor console for warnings/errors
- Test on real devices/networks
- 使用F12打开DevTools
- Console标签优先显示JavaScript错误
- Sources标签用于设置断点
- Inspector用于HTML/CSS检查
- Network标签用于请求分析
- Performance标签用于性能分析
- Memory标签用于内存泄漏检测
- 使用条件断点进行调试
- 监控控制台的警告/错误
- 在真实设备/网络上测试