Deutsch
Redirects

JavaScript redirect

A JavaScript redirect is a redirect that the browser carries out with a script, usually by setting window.location. The server first delivers an ordinary page with status 200. The visitor only reaches the new URL once the script has run.

How does it differ from an HTTP redirect?

With an HTTP redirect (301, 302, 307, 308) the information sits in the status code and the Location header. With a JavaScript redirect it sits in the page’s code, so a crawler only sees it after rendering the page. MDN names two limits of this method. It works only on clients that execute JavaScript, and it cannot be used for all resource types, for example images. In return it allows conditions, such as redirecting only if some check passes.

MDN also describes the order of execution. HTTP redirects run first, JavaScript redirects next, and <meta> refresh redirects afterwards, since they fire after the page has loaded.

Redirect types compared
TypePermanent or temporary?Request methodEffect in GoogleHow reliably Google detects it
301 Moved PermanentlypermanentPOST may become GET when followedSignal that the target should become canonicalmost reliable (server-side)
308 Permanent Redirectpermanentpreservedsame as 301most reliable (server-side)
302 FoundtemporaryPOST may become GET when followednot used as a canonical signal for the target; other signals can still get it indexedmost reliable (server-side)
307 Temporary Redirecttemporarypreservedsame as 302most reliable (server-side)
Meta refresh, instant (0 s)permanent– (redirect inside the HTML, no 3xx status code)same as 301after server-side redirects
Meta refresh, delayed (> 0 s)temporary– (redirect inside the HTML, no 3xx status code)same as 302after server-side redirects
JavaScript (location) (this term)permanent– (redirect in the browser)same as 301, but only if Google renders the pageleast reliable: if rendering fails, Google may never see it

Google orders redirect types by how likely it is to interpret them correctly: server-side redirects first, JavaScript last. Request-method behaviour is defined in RFC 9110 (section 15.4). Sources: Google Search Central: Redirects and Google Search (last updated 2026-04-14); Google Search Central: How to specify a canonical URL (redirects as canonical signal); RFC 9110: HTTP Semantics, 15.4 Redirection 3xx. Last checked: .

What does Google say?

Google’s redirect documentation lists the JavaScript location redirect among the permanent redirect types, together with 301, 308 and meta refresh with 0 seconds. The table is ordered by how likely Google is to interpret the redirect correctly, and server-side redirects come first. Google advises using JavaScript redirects only if you cannot do server-side or meta refresh redirects.

Google’s JavaScript SEO basics mention one intended use. In client-side rendered apps where meaningful HTTP status codes are hard to send, a JavaScript redirect to a URL that returns 404 (for example /not-found) avoids soft 404 errors.

Example

HTML
<script>
  window.location.href = "https://example.com/new-page/";
</script>

The server-side equivalent in nginx:

nginx
return 301 https://example.com/new-page/;

Common mistakes

  • A JavaScript redirect is used where a server-side 301 is possible.
  • JavaScript, meta refresh and HTTP redirects are active at the same time. MDN advises against mixing them, because differences can cause an infinite loop.

Sources

  1. Redirects and Google Search – Google Search Centraldevelopers.google.com
  2. JavaScript SEO basics – Google Search Centraldevelopers.google.com
  3. Redirections in HTTP – MDN Web Docsdeveloper.mozilla.org

Last updated: