How to View Source Code in Safari (Mac & iPhone)
In this article
Yes, Safari lets you view a webpage's source code, but the option is hidden by default. On a Mac, you enable a menu bar tool called Develop, then use its "Show Page Source" command. On iPhone and iPad, there is no direct built-in viewer, but you can use a bookmarklet or a third-party app. This guide covers both.
What You'll Need
- A Mac running Safari (any recent version) or an iPhone/iPad with Safari.
- For iPhone/iPad: a free bookmarklet or a third-party source viewer app from the App Store (optional).
- No special technical skills required.
Step-by-Step: Mac
- Enable the Develop menu. Open Safari and go to Safari > Settings (or Preferences in older versions). Click the Advanced tab at the top. At the bottom, check the box next to "Show features for web developers." Close the settings window.
- Open the Develop menu. You'll now see a Develop menu in the menu bar at the top of the screen, between Bookmarks and Window.
- Navigate to the page you want to inspect. Go to any website in Safari.
- Show the source. Click Develop in the menu bar, then select Show Page Source. A new panel will slide down at the bottom of the window, showing the HTML source code.
- Browse the code. You can scroll, search (press Cmd+F), and click on any line to inspect it further. The panel also shows the CSS and JavaScript associated with the page.
- Close the panel. Click the X button in the top-left corner of the panel to close it.
Step-by-Step: iPhone and iPad
Safari on iOS does not have a built-in "View Source" button, but you can use a bookmarklet that extracts the HTML and displays it in a new tab. Here's how:
- Copy the bookmarklet code. You'll need a small piece of JavaScript. A common one is:
javascript:var%20source=document.documentElement.outerHTML;var%20win=window.open('','','');win.document.write(source);(You can find this and similar snippets on trusted developer sites.) - Add a bookmark. In Safari on your iPhone, open any page, tap the Share button (the square with an arrow up), then tap Add Bookmark. Name it "View Source" and save it.
- Edit the bookmark's URL. Go to your bookmarks list (tap the book icon), find the bookmark you just created, and tap Edit in the bottom-right corner. Tap the bookmark, then tap the URL field and replace it with the bookmarklet code you copied. Tap Done.
- Use the bookmarklet. On any webpage, tap the book icon, tap the "View Source" bookmark, and a new tab will open showing the page's HTML source code.
- Alternative: use a third-party app. If the bookmarklet method feels clunky, search the App Store for "HTML source viewer" and install a reputable app. Then use Safari's Share sheet to send the page URL to that app, which will display the source.
Common Mistakes
- Forgetting to enable the Develop menu. The most common issue is that users look for "View Source" in the View menu or right-click context menu, but it only appears after you enable the Develop menu in Advanced settings.
- Using the wrong bookmarklet. If you copy a bookmarklet with line breaks or spaces, it may not work. Make sure it's a single line and starts with
javascript:. - Expecting the source to match the rendered page exactly. The source code is the raw HTML before JavaScript runs. If a page changes content dynamically, the source may not show what you see on screen.
- On iPhone, tapping the bookmarklet opens a blank tab. This often happens if the bookmarklet code was pasted incorrectly or if the site blocks pop-ups. Check the code and try again.
Tips
- Use the Develop menu for more than source. You can also access the Web Inspector, which lets you inspect individual elements, view network requests, and test responsive design.
- Search within the source. In the source panel on Mac, press Cmd+F to find specific words, like a class name or a piece of text.
- Copy the source. In the source panel, press Cmd+A to select all, then Cmd+C to copy. You can then paste it into a text editor for further analysis.
- On iPhone, save the bookmarklet to your home screen. You can add the bookmark to your Home Screen for one-tap access, though you'll still need to open it from the bookmarks menu.
- Use the Share sheet on iPhone. Some third-party apps integrate with the Share sheet, so you can send a page directly to them without copying the URL manually.
Troubleshooting
If you don't see the Develop menu after enabling it: Restart Safari (quit and reopen). If it still doesn't appear, check that you're looking at the menu bar at the top of the screen, not the toolbar. The Develop menu appears between "Bookmarks" and "Window."
If "Show Page Source" is grayed out: This can happen on certain pages that block source viewing (rare) or if you're in a private browsing window with restrictions. Try a normal window or a different page.
If the bookmarklet doesn't work on iPhone: Make sure you're using the exact code, including the javascript: prefix. Also, some sites block pop-ups, which the bookmarklet uses to open a new window. Try a different site first. If it still fails, consider using a dedicated app instead.
If the source panel is blank on Mac: Refresh the page (Cmd+R) and try again. If the page uses heavy JavaScript, the source might be empty until the page loads fully.
Frequently Asked Questions
Can I view source code on iPhone without a bookmarklet?
No, Safari on iOS does not have a built-in source viewer. The bookmarklet method is the most direct workaround, or you can use a third-party app from the App Store.
Is viewing source code legal?
Yes, viewing source code is legal. It's the same as reading a book's pages. However, copying and using that code without permission may violate copyright or terms of service.
Why does the source code look different from the rendered page?
Because the source code is the initial HTML. JavaScript then modifies the page in your browser, so what you see on screen is the result after those scripts run. To see the final DOM, use the Web Inspector's Elements tab.
Can I edit the source code and save it?
No, you can only view the source in Safari. To edit and save, you'd need to copy the code into a text editor, make changes, and save it as an HTML file. You cannot modify the live website.
Does viewing source work on all websites?
Yes, every website has source code. However, some sites may block the bookmarklet on iPhone if they prevent pop-ups, but you can usually still view source on a Mac.
What's the difference between "Show Page Source" and "Web Inspector"?
Show Page Source displays the raw HTML in a simple panel. Web Inspector is a full developer tool that lets you inspect the live DOM, CSS, network activity, and more. For basic viewing, source is enough; for debugging, use Web Inspector.