Гоголев Сергей
window.history;
history.length;
history.back();
history.forward();
history.go(-2);
history.go(2);
⇠ вернуться назад
Асинхронно получить данные
Отрендерить часть страницы (demo)
Обновить URL без перезагрузки (demo)
history.pushState(state, name, pathname);
history.pushState(null, null, '/films/warcraft');
history.pushState(null, 'Warcraft', '/films/warcraft');
var state = { keywords: 'Horde, Alliance, War' };
history.pushState(state, null, '/films/warcraft');
console.log(history.state);
// Object {keywords: 'Horde, Alliance, War'}
Состояние должно быть
Сериализуемо
Ограничено размером (firefox – 640k)
var state = { keywords: 'Horde, Alliance, War' };
history.pushState(state, null, '/films/warcraft');
addEventListener('popstate', function (event) {
console.log(document.location.pathname, event.state);
});
history.back();
'/' null
history.forward();
'/films/warcraft' {Object {keywords: "Horde, Alliance, War"}}
var newState = { activeTab: 'trailers' };
var pathname = document.location.pathname;
history.replaceState(newState, null, pathname);
history.scrollRestoration = 'manual'; // Default: auto
demo
page('/', list);
page('/films/:id', load, show);
page('*', notFound)
function load(ctx, next) {
console.log(ctx.pathname);
console.log(ctx.state);
console.log(ctx.params.id);
next();
}
Реагирует на popstate
значит на навигацию по истории
pushState на click по ссылкам
same-origin, без атрибутов target и download
Ручное управление
page.show('/films/warcraft'); // pushState
page.redirect('/films/warcraft'); // replaceState
Для орков нет другой жизни кроме войны
var selection = window.getSelection()
selection.selectAllChildren(domNode)
selection.toString()
selection.removeAllRanges()
document.execCommand('copy');
window.addEventListener('copy', function () {});
window.addEventListener('paste', function (event) {
event.clipboardData.getData('text/html');
event.clipboardData.getData('text/plain');
});
<input id="message">
<button data-clipboard-target="#message"></button>
var clipboard = new Clipboard('button');
clipboard.on('success', function(event) {
var text = event.text;
event.clearSelection();
});
<div contenteditable></div>
<div contenteditable spellcheck></div>
document.execCommand('bold')
document.execCommand('createLink', false, 'https://ya.ru/')
document.execCommand('insertHTML', false, '<p>Awesome!</p>');
document.queryCommandSupported('createLink');
Manipulating the browser history
Manipulating history for fun & profit
Selection (MDN)
Document.execCommand() (MDN)
Content Editable (MDN)