web api instance method
NavigationPrecommitController: redirect() method
The redirect() method of the
NavigationPrecommitController interface redirects the browser to a specified URL and specifies history behavior and any desired state information.
Syntax
redirect(url, options)
Parameters
url- : The URL to redirect to.
optionsOptional- : An options object, the properties of which can include:
stateOptional- : Contains any state information you want to pass along with the navigation; for example, for logging or tracking purposes. This can be represented by any value type. The state for the navigation can subsequently be retrieved via the
getState()method of the resulting history entry.
- : Contains any state information you want to pass along with the navigation; for example, for logging or tracking purposes. This can be represented by any value type. The state for the navigation can subsequently be retrieved via the
historyOptional- : An enumerated value that specifies how this redirect should be added to the navigation history. It can take one of the following values:
auto- : The default value, which lets the browser decide how to handle it:
- If the original navigation occurred as a result of a
navigate()call, the value will be whatever was specified in thenavigate()call’shistoryoption. - Otherwise, the value used is usually
push, but it will becomereplaceif the redirect points to the same URL as the pre-navigation URL.
- If the original navigation occurred as a result of a
- : The default value, which lets the browser decide how to handle it:
push- : Adds a new
NavigationHistoryEntryto the navigation history, and clears any available forward navigation (that is, if the user previously navigated to other locations, then used the back button to return back through the history before initiating the navigation that caused the redirect).
- : Adds a new
replace- : Replaces the
currentEntrywith the resulting newNavigationHistoryEntry.
- : Replaces the
- : An enumerated value that specifies how this redirect should be added to the navigation history. It can take one of the following values:
- : An options object, the properties of which can include:
[!NOTE] The
redirect()method can convert the history behavior betweenauto,push, andreplace, but it cannot turn atraversenavigation into apush/replacenavigation and vice versa.
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMException- : Thrown if:
- The originating
NavigateEventwas not intercepted. - The
navigationTypeis notpushorreplace.
- The originating
- : Thrown if:
SyntaxErrorDOMException- : Thrown if the specified
urlis invalid.
- : Thrown if the specified
SecurityErrorDOMException- : Thrown if the current document is unable to have its URL rewritten to the provided redirect
url.
- : Thrown if the current document is unable to have its URL rewritten to the provided redirect
Examples
See the main NavigationPrecommitController page for an example.
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.