Post

Debugging in the Browser

Using the Chrome browser for debugging website errors is a common practice among developers due to its powerful built-in developer tools. Here’s a step-by-step guide to help you get started: Accessing Developer Tools

Open Developer Tools:

Right-click on the webpage and select Inspect. Or press Ctrl + Shift + I (Windows/Linux) or Cmd + Option + I (Mac).

Main Panels in Developer Tools

Elements Panel:

Inspect and Edit HTML/CSS: View and edit the HTML structure and CSS styles of your page. Find Elements: Use the magnifying glass icon to select elements on the page and inspect their properties.

Console Panel:

JavaScript Debugging: View and debug JavaScript errors and log messages. Execute Commands: Run JavaScript code directly in the context of the page. View Errors and Warnings: Look for error messages and warnings that can provide clues about issues.

Sources Panel:

JavaScript Debugging: Set breakpoints, step through code, and watch expressions. View Source Files: Browse the files that are loaded by your webpage, including HTML, CSS, and JavaScript.

Network Panel:

Monitor Network Requests: See all network requests made by the page, including HTTP requests and responses. Analyze Load Times: Check how long resources take to load and identify bottlenecks. Check for Errors: Look for failed requests or slow-loading resources.

Performance Panel:

Performance Profiling: Record and analyze the runtime performance of your application. Identify Bottlenecks: Understand which operations are slowing down your page.

Application Panel:

Inspect Storage: View and manage storage, including cookies, local storage, and session storage. Service Workers and Cache: Inspect service workers, the application cache, and other web app-related features.

Security Panel:

Check Security Issues: Ensure your site is served over HTTPS and view any security issues.

Debugging Common Issues

HTML/CSS Issues:

Use the Elements panel to inspect the HTML structure and CSS styles. Modify the styles directly in the panel to see how changes affect the page in real-time.

JavaScript Errors:

Check the Console panel for any JavaScript errors or warnings. Use the Sources panel to set breakpoints and step through the code to understand the flow and identify where things go wrong.

Network Problems:

Use the Network panel to monitor network requests and responses. Look for requests that fail or take a long time to complete and investigate the causes.

Performance Issues:

Record a performance profile using the Performance panel to see detailed metrics about page load and runtime performance. Identify and address any slow functions, rendering issues, or other bottlenecks.

Debugging JavaScript HTTP Requests (XHR / Fetch)

Any request JavaScript makes after the page loads (fetch(), XMLHttpRequest, axios, jQuery $.ajax) shows up in the Network panel. Filter to just those to cut out the noise from images, CSS and fonts.

1
2
3
4
// Example request to debug
fetch('/wp-json/wp/v2/posts?per_page=3')
  .then(res => res.json())
  .then(data => console.log(data));

Open the XHR view

BrowserSteps
ChromeDevTools > Network > click the Fetch/XHR filter
FirefoxDevTools > Network > click the XHR filter (includes fetch() calls)

Then:

  1. Open the Network panel before triggering the action (click the button, submit the form, scroll).
  2. Enable Preserve log (Chrome) / Persist Logs (Firefox) so requests survive a page navigation or form redirect.
  3. Trigger the action. A new row appears for each request.

Read a request

Click a row to open its details.

What you wantChrome tabFirefox tab
URL, method, status code, request/response headersHeadersHeaders
Data sent (POST body, query params)PayloadRequest
Data returnedPreview (formatted) / Response (raw)Response
Which JS code made the requestInitiatorStack Trace
Where the time wentTimingTimings

What to check first

  • Status column: 200 ok, 301/302 redirect, 400 bad data sent, 401/403 not logged in or not allowed, 404 wrong URL, 500 server error (check the server logs).
  • Method: wrong GET vs POST is a common cause of 405 Method Not Allowed.
  • Payload: is the data you think you are sending what is actually sent? Empty or undefined values are common.
  • Response: an HTML page instead of JSON usually means a login redirect or a PHP error.
  • Red row with no status: the request never completed. Usually a CORS block, a blocked request (ad blocker), or the server is down. Check the Console for the exact error.
  • Two requests for one call: the first is a CORS preflight (OPTIONS). If it fails, the real request is never sent.

Useful right-click options on a request

  • Copy as cURL: Chrome Copy > Copy as cURL, Firefox Copy Value > Copy as cURL. Paste into a terminal to reproduce the request outside the browser. See the cURL cheatsheet.
  • Resend: Chrome Replay XHR, Firefox Resend (or Edit and Resend to change headers and body first).
  • Block request URL: Chrome Block request URL, Firefox Block URL. Reload to see how the page behaves when that request fails.

Pause when a request fires

Break on the line of JavaScript that sends the request.

  • Chrome: Sources panel > right sidebar > XHR/fetch Breakpoints > + > enter part of the URL (e.g. wp-json).
  • Firefox: Debugger panel > right sidebar > XHR Breakpoints > + > enter part of the URL.

Execution pauses at the fetch() / send() call. Use the call stack to see what triggered it.

Tips and Tricks

Preserve Log: In the Console panel, enable the “Preserve log” option to keep the log messages between page loads. Throttle Network: Use the Network panel to simulate different network conditions (e.g., slow 3G) to test your site’s performance under various conditions. Mobile View: Toggle the device toolbar (Ctrl + Shift + M or Cmd + Shift + M) to test how your site behaves on different screen sizes and devices.

By familiarizing yourself with these tools and techniques, you can effectively debug and optimize your website using Chrome’s developer tools.

This post is licensed under CC BY 4.0 by the author.