-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
index.md
52 lines (34 loc) · 3.33 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
title: History.replaceState()
slug: Web/API/History/replaceState
---
{{APIRef("History API")}}
Метод **`History.replaceState()`** изменяет текущую запись в истории, заменяя её на значения `stateObj`, `title` и `URL`, передаваемые в параметрах метода. Данный метод особенно полезен, когда вы хотите обновить объект состояния или URL текущей записи в истории в ответ на какое-то действие пользователя.
## Синтаксис
```
history.replaceState(stateObj, title, [url])
```
### Параметры
- `stateObj`
- : Объект состояния – это JavaScript-объект, связанный с записью в истории, переданной в метод `replaceState()`. Объект состояния может быть `null`.
- `title`
- : Заголовок - [все браузеры, кроме Safari, на данный момент игнорируют этот параметр](https://github.com/whatwg/html/issues/2174), но могут начать использовать в будущем. Ввиду будущих изменений метода, безопасным решением является передача пустой строки. В качестве альтернативы вы можете передать короткий заголовок для состоянии, в которое переходите.
- `url` {{optional_inline}}
- : URL-адрес записи в истории. Новый URL должен вести на тот же домен, протокол и порт, иначе pushState() выдаст исключение.
## Примеры
Предположим, на странице `http://mozilla.org/foo.html` выполняется следующий JavaScript-код:
```
let stateObj = { foo: "bar" }
history.pushState(stateObj, "page 2", "bar.html")
```
Объяснение этих двух строк можно найти в приведённом выше разделе _[пример метода pushState()](#Example_of_pushState_method)_.
Далее, предположим, на странице `http://mozilla.org/bar.html` выполняется JavaScript-код:
```
history.replaceState(stateObj, "page 3", "bar2.html")
```
Это приведёт к тому, что в URL-строке отобразится адрес `http://mozilla.org/bar2.html`, но браузер не станет сразу загружать `bar2.html` и даже не станет проверять наличие этой страницы `bar2.html`.
Теперь предположим, что пользователь переходит по адресу `http://www.microsoft.com`, а затем нажимает на кнопку **"Назад"**. В этом случае в URL-строке отобразится `http://mozilla.org/bar2.html`. Если же пользователь снова нажмёт на кнопку **"Назад"**, в URL-строке отобразится `http://mozilla.org/foo.html` и полностью обойдёт `bar.html`.
## Спецификации
{{Specifications}}
## Совместимость с браузерами
{{Compat}}