About Safari Developer Tools

Safari 4.0 and later includes built-in tools such as Web Inspector, shown in Figure I-1, to help you prototype, analyze, debug, and optimize websites and web apps. Safari 5.0 and later has additional tools you can use to develop and debug Safari extensions.

Figure I-1  Web Inspector
The Web Inspector's window

At a Glance

Safari has tools for prototyping HTML, CSS, and JavaScript; tools for interactively inspecting and debugging elements, attributes, properties, and styles; an integrated JavaScript debugger; optimization tools such as a latency and download timeline; and a JavaScript and CSS profiler.

These tools are built into Safari on OS X, iOS, and Windows. You can enable them in other WebKit-based apps.

Enable the Developer Tools in Safari Preferences

Enable the developer tools in the Advanced pane of Safari preferences:

The Advanced pane of Safari preferences window.The Advanced pane of Safari preferences window.

Speed Up Prototyping by Interactively Testing for Errors

Show Web Inspector’s console while prototyping your website to quickly spot HTML and JavaScript structural and syntactic errors. The console shows you errors and warnings, highlights the line number of the problem in your source file, and tells you how Safari dealt with the error (by ignoring an extra closing tag, for example). For details, see “Using the Error Console,” “Viewing Issues,” and “Using the Console to Prototype JavaScript.”

Safari also comes with a Snippet Editor, where you can type in HTML, CSS, and JavaScript snippets and see them evaluated interactively. No more having to create a dummy HTML page to test an element or a JavaScript function—just type in the part you’re working on and see the results. When it works as you want it to, copy and paste it into your actual webpage. For details, see “Using the Snippet Editor.”

If your website has different code paths for different browsers, have Safari give different user-agent strings to test the code branches. Then, verify the behavior changes for Safari, Internet Explorer, Opera, Chrome, or Safari on iOS without having to switch browsers, operating systems, or devices. When you’re ready to open your website in another application, make the switch from inside Safari, without having to quit, open another application, and navigate to your site. For details, see “Changing the User Agent String” and “Switching to Another App.”

Debug Your HTML and CSS Interactively Using Web Inspector

The Error Console finds and identifies syntactic and structural errors, but sometimes your website doesn’t look or behave as you want, even though the syntax and structure are legal. That’s when you need Web Inspector. Web Inspector shows you the DOM as it exists in memory—for a static HTML page, that’s often identical to the page source, but for websites that modify the DOM using JavaScript and CSS, it can be very different.

With Web Inspector, you can:

Use Web Inspector to Debug JavaScript Interactively

Web Inspector includes tools to help you set breakpoints, pause, inspect variable values, see the call stack, log messages and data to the console, set variable values and continue, and enter JavaScript on the fly to test it—all with autocompletion of function, property, and variable names to speed you along.

Get Help with Cookies, Local Storage, the Application Cache, and HTML5 Client-Side Databases

Use Web Inspector’s Storage navigator to inspect cookies, local key-value storage, the offline application cache, and even client-side relational databases created with HTML5. All local data is displayed in editable data grids. You can perform actions or enter data in the webpage and see the results in the grid, or enter data interactively in the grid itself. Issue SQL queries right from Web Inspector, with auto-completion of SQL functions and database field names. Inspect the contents of the HTML5 application cache to better understand offline application behavior.

Use the Instrument Navigator

Web Inspector’s Instrument navigator shows you exactly where time is spent, so you know where work is needed before you change anything. Once you know exactly where the bottlenecks are, you can make changes interactively without leaving Safari, optimizing your site—and fixing any resulting problems—before you modify your working source.

The Instrument navigator shows all the resources your site uses—HTML files, JavaScript files, CSS style sheets, server-side output, images, fonts, and media files—in a timeline view, showing when each resource was requested, the latency for each request, when the first byte of the resource arrived, and when the resource finished loading. At a glance, see if your website is hung by a slow server script, bandwidth-choking file sizes, network latency problems, or unexpected dependencies such as a resource that is not requested until after a script executes.

The Instrument navigator lets you profile your website in real time, showing not only the latency and load time for each resource, but when events occur, as well as the time spent executing scripts, parsing, and rendering. Complex interactions—a script that changes the DOM, causing a resource to load, and a table to be rerendered, for example—are laid out clearly. Header details and memory usage over time are also shown.

If the timeline shows that significant time is spent in your scripts, use the Profiles section of the Instrument navigator to determine where. Moving a single function call from the inside of a loop to the outside of the loop can sometimes have a tremendous impact. Running a profile takes the guesswork out of optimizing JavaScript. A profile shows you how much time is spent in each function, including dependent functions, and how many times each function is called.

See the output in milliseconds or percent of execution time. Sort by execution time. Then spend your effort optimizing code that you know is going to make a significant difference—modify only functions that take a proportionally long time to execute or functions that are called many times.

Be a Power User

The Safari developer tools include dozens of keyboard and mouse shortcuts to speed up common operations, from opening Web Inspector to cycling through auto-completion suggestions. See the shortcuts table in “Keyboard and Mouse Shortcuts” to boost your productivity.

Enable, Build, and Debug Extensions

Use Extension Builder to create extensions. You need to join the Safari Developer Program to install the extensions you create—you can’t install an extension unless it has a signed certificate. Go to developer.apple.com to join the program.

When you’re ready to build an extension, read Safari Extensions Development Guide, and refer to Safari Extensions Reference. If you’ve already developed extensions for other browsers, see Safari Extensions Conversion Guide for time-saving tips.

The full set of developer tools for inspecting, modifying, and optimizing HTML, CSS, and JavaScript works on extensions. Once you’ve built and installed an extension, you can use Web Inspector to debug and optimize:

See Also


Did this document help you? Yes It's good, but... Not helpful...