{
  "doc": {
    "id": "api/commands/hash",
    "title": "cy.hash()",
    "description": "Get the current URL hash of the page that is currently active in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/hash.md",
    "version": "f60251bf707b0077a9535579c9f90d615440cc35",
    "updated_at": "2026-10-16T10:56:58.670Z",
    "headings": [
      {
        "id": "api/commands/hash#hash",
        "text": "hash",
        "level": 1
      },
      {
        "id": "api/commands/hash#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/hash#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/hash#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/hash#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/hash#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/hash#no-args",
        "text": "No Args",
        "level": 3
      },
      {
        "id": "api/commands/hash#assert-that-hash-is-users-1-given-remote-url-http-localhost-8000-app-users-1",
        "text": "Assert that hash is #/users/1 given remote URL: http://localhost:8000/app/#/users/1",
        "level": 4
      },
      {
        "id": "api/commands/hash#assert-that-the-hash-matches-via-regexp",
        "text": "Assert that the hash matches via RegExp",
        "level": 4
      },
      {
        "id": "api/commands/hash#assert-that-the-hash-is-empty-after-closing-a-view",
        "text": "Assert that the hash is empty after closing a view",
        "level": 4
      },
      {
        "id": "api/commands/hash#hash-based-routing",
        "text": "Hash-based routing",
        "level": 3
      },
      {
        "id": "api/commands/hash#wait-for-a-hash-route-to-change-after-a-click",
        "text": "Wait for a hash route to change after a click",
        "level": 4
      },
      {
        "id": "api/commands/hash#query-strings-inside-a-hash-route",
        "text": "Query strings inside a hash route",
        "level": 4
      },
      {
        "id": "api/commands/hash#in-page-anchor-links",
        "text": "In-page anchor links",
        "level": 3
      },
      {
        "id": "api/commands/hash#assert-the-hash-after-following-a-jump-link",
        "text": "Assert the hash after following a jump link",
        "level": 4
      },
      {
        "id": "api/commands/hash#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/hash#cross-origin-pages-and-cy-hash",
        "text": "Cross-origin pages and cy.hash()",
        "level": 3
      },
      {
        "id": "api/commands/hash#percent-encoded-characters-in-the-hash",
        "text": "Percent-encoded characters in the hash",
        "level": 3
      },
      {
        "id": "api/commands/hash#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/hash#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/hash#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/hash#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/hash#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/hash#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/hash#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "hash"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Get the current URL hash of the page that is currently active."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` yields the same value as "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/location.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.location('hash')`"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.hash()\ncy.hash(options)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.hash() // Get the url hash"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('a').hash() // Ignores the link, yields the page's hash"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` reads the page's URL, not an element's `href`. To check the hash a link points to, assert on the link's `hash` property instead:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('a').should('have.prop', 'hash', '#/users/1')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "options (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass in an options object to change the default behavior of `cy.hash()`."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "cy.hash( options )"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Option"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Default"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`log`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`true`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Displays the command in the "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/open-mode.md#Command-Log",
                    "children": [
                      {
                        "type": "text",
                        "value": "Command log"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`timeout`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/configuration.md#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "`defaultCommandTimeout`"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Time to wait for `cy.hash()` to resolve before "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "timing out"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "When the current URL contains a hash, `cy.hash()` yields the current URL's hash (including the `#` character)."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "When the current URL does not contain a hash, or ends in a `#` with nothing after it, `cy.hash()` yields an empty string."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.hash()` is a query, and it is safe to chain further commands."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` yields the part of the URL labeled `hash` in these diagrams:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "In a hash-based route, a query string after the `#` is part of the hash:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "No Args"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Assert that hash is `#/users/1` given remote URL: `http://localhost:8000/app/#/users/1`"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// yields #/users/1\ncy.hash().should('eq', '#/users/1') // => true"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Assert that the hash matches via RegExp"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "index.html"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<ul id=\"users\">\n  <li>\n    <a href=\"#/users/8fc45b67-d2e5-465a-b822-b281d9c8e4d1\">Fred</a>\n  </li>\n</ul>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "spec.cy.js"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#users li').find('a').click()\ncy.hash().should('match', /users\\/.+$/) // => true"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Assert that the hash is empty after closing a view"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "A link to a bare `#` clears the hash, so `cy.hash()` yields an empty string."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "index.html"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<nav>\n  <a href=\"#/users/1\">Open profile</a>\n  <a href=\"#\">Close profile</a>\n</nav>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "spec.cy.js"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.contains('a', 'Open profile').click()\ncy.hash().should('eq', '#/users/1')\n\ncy.contains('a', 'Close profile').click()\ncy.hash().should('be.empty')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Hash-based routing"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Wait for a hash route to change after a click"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` retries until its assertions pass, so the test waits for your application's router to update the URL without a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wait.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.wait()`"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "index.html"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<nav>\n  <a href=\"#/\">Home</a>\n  <a href=\"#/settings\">Settings</a>\n</nav>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "spec.cy.js"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.contains('a', 'Settings').click()\ncy.hash().should('eq', '#/settings')"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Query strings inside a hash route"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "In a hash-based route, everything after the `#` belongs to the hash, including a `?` query string. "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/location.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.location('search')`"
              }
            ]
          },
          {
            "type": "text",
            "value": " reads only the query string before the `#`."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// given remote URL: http://localhost:8000/#/users/1?tab=profile\ncy.hash().should('eq', '#/users/1?tab=profile')\ncy.location('search').should('eq', '')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "In-page anchor links"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Assert the hash after following a jump link"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "index.html"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<a href=\"#pricing\">Pricing</a>\n\n<section id=\"pricing\" style=\"margin-top: 2000px\">\n  <h2>Pricing</h2>\n</section>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "spec.cy.js"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.contains('a', 'Pricing').click()\ncy.hash().should('eq', '#pricing')\ncy.window().its('scrollY').should('be.greaterThan', 0)"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Notes"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Cross-origin pages and `cy.hash()`"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` reads the URL from the browser rather than from the page's `window`, so it yields the hash after your application navigates to another origin, without wrapping the command in "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/origin.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.origin()`"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Percent-encoded characters in the hash"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.hash()` yields the hash as the browser's URL parser serializes it, so characters such as spaces are percent-encoded. A hash set to `#first name` yields `#first%20name`."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.hash().should('eq', '#first%20name')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To compare against the readable text, decode the hash inside a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/should.md",
            "children": [
              {
                "type": "text",
                "value": "`.should()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " callback, which keeps the retry:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.hash().should((hash) => {\n  expect(decodeURIComponent(hash)).to.eq('#first name')\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.hash()` requires being chained off of `cy`."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assertions"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.hash()` will automatically "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " until all chained assertions have passed"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.hash()` can time out waiting for assertions you've added to pass."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Assert that the hash matches `#users/new`"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.hash().should('eq', '#users/new')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The commands above will display in the Command Log as:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When clicking on `hash` within the Command Log, the console outputs the following:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "History"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Version"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Changes"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#15-18-1",
                    "children": [
                      {
                        "type": "text",
                        "value": "15.18.1"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Reads the URL through the automation client in experimental WebKit"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#15-0-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "15.0.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Reads the URL through the browser's automation client instead of the page's `window` in Chromium-based browsers and Firefox"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/location.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`cy.location()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/url.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`cy.url()`"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 915
}