Run snippets of JavaScript Stay organized with collections Save and categorize content based on your preferences.
If you find yourself running the same code in theConsole repeatedly, consider saving the code as a snippet instead. Snippets have access to the page's JavaScript context. They are an alternative tobookmarklets.
You can author snippets in theSources panel and run them on any page and in incognito mode.
Note: DevTools stores snippets as your local preferences. DevTools doesn'tsync snippets with settings and they can't be accessed via the file system.For example, the screenshot below shows the DevTools documentation homepage on the left and some snippet source code in theSources> Snippets pane on the right.
Here's the snippet source code thatlogs some message and replaces the homepage's HTML body with a<p>
element that contains the message:
console.log('Hello, Snippets!');document.body.innerHTML='';constp=document.createElement('p');p.textContent='Hello, Snippets!';document.body.appendChild(p);
When you click theRun button, theConsole drawer pops up to display the
Hello, Snippets!
message that the snippet logs, and the page's content changes.
Open the Snippets pane
TheSnippets pane lists your snippets. To edit a snippet, open it in one of two ways:
Navigate toSources>
> Snippets.
From theCommand Menu:
- PressControl+Shift+P (Windows/Linux) orCommand+Shift+P (Mac) to open theCommand Menu.
- Start typing
Snippets
, selectShow Snippets, and pressEnter.
TheSources>Snippets pane shows you a list of snippets you saved, empty in this example.
Create snippets
You can create snippets in theSnippets pane or by running the corresponding command from theCommand Menu anywhere in DevTools.
TheSnippets pane sorts your snippets in alphabetical order.
Create a snippet in the Sources panel
- Open theSnippets pane.
- Click
New snippet.
Enter a name for your snippet and pressEnter to save.
Create a snippet from the Command Menu
- Focus your cursor anywhere inside of DevTools.
- PressControl+Shift+P (Windows/Linux) orCommand+Shift+P (Mac) to open theCommand Menu.
Start typing
Snippet
, selectCreate new snippet, then pressEnter to run the command.
SeeRename snippets if you'd like to give your new snippet a custom name.
Edit snippets
- Open theSnippets pane.
In theSnippets pane, click the name of the snippet that you want to edit. TheSources panel opens it in theCode Editor.
Use theCode Editor to edit code in your snippet. An asterisk next to the snippet name means that you haven't saved your changes yet.
PressControl+S (Windows/Linux) orCommand+S (Mac) to save.
Run snippets
Similar to creating a snippet, you can run it both in theSnippets pane and from theCommand Menu.
Run a snippet in the Sources panel
- Open theSnippets pane.
- Click the name of the snippet you want to run. TheSources panel opens it in theCode Editor.
Click
Run in the action bar at the bottom of the editor,or pressControl+Enter (Windows/Linux) orCommand+Enter (Mac).
Run a snippet from the Command Menu
- Focus your cursor anywhere inside of DevTools.
- PressControl+O (Windows/Linux) orCommand+O (Mac) to open theCommand Menu.
Type the
!
character followed by the name of the snippet that you want to run.PressEnter to run the snippet.
Rename snippets
- Open theSnippets pane.
- Right-click the snippet name and selectRename.
Delete snippets
- Open theSnippets pane.
- Right-click the snippet name and selectRemove.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2015-10-12 UTC.