Run snippets of JavaScript

Kayce Basques
Kayce Basques
Sofia Emelianova
Sofia Emelianova

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.

The DevTools documentation homepage before running the snippet. The Run button is highlighted.

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.Run button, theConsole drawer pops up to display theHello, Snippets! message that the snippet logs, and the page's content changes.

The homepage after running the snippet.

Open the Snippets pane

TheSnippets pane lists your snippets. To edit a snippet, open it in one of two ways:

  1. Navigate toSources>More tabs.> Snippets.

    The More tabs menu on the Sources pane.

  2. From theCommand Menu:

    1. PressControl+Shift+P (Windows/Linux) orCommand+Shift+P (Mac) to open theCommand Menu.
    2. Start typingSnippets, selectShow Snippets, and pressEnter.

    Selecting Show Snippets from the Command Menu.

TheSources>Snippets pane shows you a list of snippets you saved, empty in this example.

An empty Snippets pane.

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

  1. Open theSnippets pane.
  2. ClickNew snippet.New snippet.
  3. Enter a name for your snippet and pressEnter to save.

    Naming a snippet.

Create a snippet from the Command Menu

  1. Focus your cursor anywhere inside of DevTools.
  2. PressControl+Shift+P (Windows/Linux) orCommand+Shift+P (Mac) to open theCommand Menu.
  3. Start typingSnippet, selectCreate new snippet, then pressEnter to run the command.

    Selecting Create new snippet from the Command Menu.

SeeRename snippets if you'd like to give your new snippet a custom name.

Edit snippets

  1. Open theSnippets pane.
  2. In theSnippets pane, click the name of the snippet that you want to edit. TheSources panel opens it in theCode Editor.

    A snippet opened in the Code Editor.

  3. 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.

    An asterisk next to the snippet name that indicates unsaved code.

  4. 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

  1. Open theSnippets pane.
  2. Click the name of the snippet you want to run. TheSources panel opens it in theCode Editor.
  3. ClickRun.Run in the action bar at the bottom of the editor,or pressControl+Enter (Windows/Linux) orCommand+Enter (Mac).

    The Run button.

Run a snippet from the Command Menu

  1. Focus your cursor anywhere inside of DevTools.
  2. PressControl+O (Windows/Linux) orCommand+O (Mac) to open theCommand Menu.
  3. Type the! character followed by the name of the snippet that you want to run.

    Running a snippet from the Open Menu.

  4. PressEnter to run the snippet.

Rename snippets

  1. Open theSnippets pane.
  2. Right-click the snippet name and selectRename.

Delete snippets

  1. Open theSnippets pane.
  2. 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.