window.history
. History has several methods, events, and properties that we can control from JavaScript. Each page of a tab (Document object) is an object of the History collection. Each history element consists of a URL and / or a state object (a state object); it can have a title (title), a Document object, form data, a scroll position, and other information associated with the page.
window.history.length
: Number of records in the current history sessionwindow.history.state
: Returns the current history objectwindow.history.go(n)
: A method that allows you to walk through history. An argument is passed as an argument, relative to the current position. If 0 is transmitted, the current page will be updated. If the index goes beyond history, then nothing will happen.window.history.back()
: Method identical to go(-1)
window.history.forward()
: Method identical to go(1)
window.history.pushState(data, title [, url])
: Adds a history item.window.history.replaceState(data, title [, url])
: Updates the current history item history.go(-2)
history.pushState
:
history.pushState({foo: 'bar'}, 'Title', '/baz.html')
history.replaceState
:
history.replaceState({foo: 'bat'}, 'New Title')
data-*
attributes to store the header of each image and use the dataset property to get this property:
<li class="photo"> <a href="crab2.png" data-note="Grey crab!">crab2.png</a> </li>
src
attribute dynamically. This acceleration creates one problem - it breaks the back button, so you cannot go forward or backward through the pictures.
location
document is updated (it contains the unique URL of the image). This means that we can use the back button to crawl our images, while in the address bar we will have a direct link to the picture, which we can bookmark or send to someone.
bindEvents
method hangs handlers for the popstate
event, which is called when the user navigates through the history and allows the application to update its state.
window.addEventListener('popstate', function(e){ self.loadImage(e.state.path, e.state.note); }, false);
event
object that is passed to the popstate
event popstate
has a state
property — this is the data that we passed as the first argument to pushState
or replaceState
.
click
event on a div, which represents our file structure. Using the event delegation, we open or close a folder or load a picture (with adding an entry to the history). We look at the parent's className
in order to understand which element we clicked on:
dir.addEventListener('click', function(e){ e.preventDefault(); var f = e.target; // if (f.parentNode.classList.contains('folder')) { // self.toggleFolders(f); } // else if (f.parentNode.classList.contains('photo')){ note = f.dataset ? f.dataset.note : f.getAttribute('data-note'); // self.loadImage(f.textContent, note); // history.pushState({note: note, path:f.textContent}, '', f.textContent); } }, false);
loadImage: function(path, note){ img.src = path; h2.textContent = note; }
History
object. We use pushState
to add a history item and a popstate event to update the page content. In addition, when clicking on a picture, we get its address in the address bar, which we can save or send to someone.
Source: https://habr.com/ru/post/123106/